Skip to content

Instantly share code, notes, and snippets.

@jugyo
Created January 14, 2010 05:12
Show Gist options
  • Save jugyo/276899 to your computer and use it in GitHub Desktop.
Save jugyo/276899 to your computer and use it in GitHub Desktop.
class Hash
def to_sorted_hash
class << self
alias_method :__keys, :keys
def keys
self.__keys.sort_by {|k| k.to_s }
end
def values
self.keys.map {|k| self[k] }
end
end
self
end
end
sorted_hash = {:b => 'b', :a => 'a'}.to_sorted_hash
p sorted_hash.keys #=> [:a, :b]
p sorted_hash.values #=> ["a", "b"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment