Created
February 10, 2012 16:51
-
-
Save kbarber/1790829 to your computer and use it in GitHub Desktop.
Blog: YAML/hash sort 1
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_yaml1(obj) | |
YAML::quick_emit(obj) do |out| | |
out.map(obj.taguri, obj.to_yaml_style) do |map| | |
obj.keys.sort.each do |k| | |
v = obj[k] | |
map.add(k, v) | |
end | |
end | |
end | |
end | |
puts sort_yaml1(a) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment