From: Ilan Berci Date: 2008-01-29T06:31:30+09:00 Subject: Re: Hash Sorting Nico Ritsche wrote: > James Tucker wrote: >> On 28 Jan 2008, at 17:45, Nico Ritsche wrote: >>> >>> Any ideas? >> >> hash.to_a.sort > > Tried that, the result is the same, no sorting is happening at all. > Same as if I just do hash.to_a. James is correct, the array will be sorted but the hash will remain "unsorted". Hashes are random access unsorted collections.. you can't "sort" a hash.. you can however as James suggested, move the elements to a hash and then sort the result any way you want.. irb(main):001:0> h = {1=>2,3=>4,5=>6} => {5=>6, 1=>2, 3=>4} irb(main):002:0> h.to_a.sort => [[1, 2], [3, 4], [5, 6]] irb(main):003:0> h.to_a.sort_by {|a,b| b} => [[1, 2], [3, 4], [5, 6]] irb(main):004:0> -- Posted via http://www.ruby-forum.com/.