Skip to content

Instantly share code, notes, and snippets.

@kbarber
Created February 10, 2012 16:54
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/1790855 to your computer and use it in GitHub Desktop.
Save kbarber/1790855 to your computer and use it in GitHub Desktop.
Blog: YAML/hash sort 2
#!/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