From: Mark Probert Date: 2003-01-03T12:16:29+09:00 Subject: Re: sort the hash keys Hi. At 12:04 PM 1/3/2003 +0900, Gavin wrote: >On Friday, January 3, 2003, 1:52:24 PM, TOTO wrote: > > > How can I sort the keys of a Hash so that they can be outputed > > according to the keys sorted order? > >A Hash is explicitly a non-sorted data structure. If you just want >the keys in sorted order, use: > > hash.keys.sort > >Look at other methods in Hash (e.g. sort) for more info. You can also use hash.sort directly, however, be careful. It returns an array of [key, value]. So to sort and iterate over the hash hash_a do hash_a.each { |item| key = item[0] value = item[1] # do stuff ... } Regards, -mark.