From: Robert Klemme Date: 2003-07-02T20:37:33+09:00 Subject: Re: Sorting a Hash "Osuka" schrieb im Newsbeitrag news:9411fd36.0307010845.2eb1149d@posting.google.com... > > myhash.sort {|x,y| x[1] <=> y[1]} > > > > => [["Ayumi Hamasaki", 1], ["L'Arc~en~ciel", 1], ["Two-Mix", 1], > > ["Shazna", 2], ["Zone", 2]] > > > > (you can see all the 1's come before the 2's) > > Arrg I did tried this and the to_a approach but i forgot something way > too simple! if myhash.sort {|x,y| x[1] <=> y[1]} does return the > sorted array but doesn't actually affects the given array so I have to > assign the returned value to the hash I'm using, how come I miss it!!! > all the problem was here!! No, you don't "assign the returned value to the hash", because Hashes are *always* unsorted. The sort method allows you to process them in order but you can't change the internal order in the hash. Of course you can store the nested array you get from sort. But this is not a Hash any more. Cheers robert