Skip to content

Instantly share code, notes, and snippets.

@kbarber
Created February 10, 2012 16:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kbarber/1790829 to your computer and use it in GitHub Desktop.
Save kbarber/1790829 to your computer and use it in GitHub Desktop.
Blog: YAML/hash sort 1
#!/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