I use this code for sorting Hash; I've got no idea how its works. please explain to me:
def foo(hash)
Hash[hash.sort]
end
irb(main):001:0> h = {1=>'z', 3=>'x', 2=>'y'}
=> {1=>"z", 3=>"x", 2=>"y"}
irb(main):002:0> Hash[h.sort]
=> {1=>"z", 2=>"y", 3=>"x"}
irb(main):003:0>
Hash[hash.sort_by(&:first)], wherehash.sort_by(&:first)sorts the key-value pairs by key. If the hash is large this may be faster than usingsort.