Created
February 10, 2012 16:54
-
-
Save kbarber/1790855 to your computer and use it in GitHub Desktop.
Blog: YAML/hash sort 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'yaml' | |
a = { | |
"a" => "b", | |
"e" => "b", | |
"c" => "b", | |
"p" => "b", | |
"z" => "b", | |
"x" => "b", | |
"d" => "b", | |
} | |
def sort_yaml2(obj) | |
arr = obj.sort | |
out = "---\n" | |
arr.each do |element| | |
entry = {element[0] => element[1]} | |
out += entry.to_yaml.to_a[1..-1].join + "\n" | |
end | |
out | |
end | |
puts sort_yaml2(a) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment