Skip to content

Instantly share code, notes, and snippets.

@jaredmoody
Created December 19, 2013 20:32
Show Gist options
  • Save jaredmoody/8045790 to your computer and use it in GitHub Desktop.
Save jaredmoody/8045790 to your computer and use it in GitHub Desktop.
def deeply_sort_hash(object)
return object unless object.is_a?(Hash)
hash = RUBY_VERSION >= '1.9' ? Hash.new : ActiveSupport::OrderedHash.new
object.each { |k, v| hash[k] = deeply_sort_hash(v) }
sorted = hash.sort { |a, b| a[0].to_s <=> b[0].to_s }
hash.class[sorted]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment