From: Mark Probert Date: 2003-01-04T02:57:55+09:00 Subject: Re: sort the hash keys Hi, Guy. At 11:42 PM 1/3/2003 +0900, Guy wrote: > >>>>> "M" == Mark Probert writes: > >M> My mistake. That should have been > >M> hash_a.sort { |item| # item is array [key, value] > > I don't understand, sorry, but Hash#sort will use the block to sort the > hash and item is [[key0, value0], [key1, value1]] (I have to stop sprouting when I am sick .. :-( ) What I -meant- to say was "hash_a.sort.each". So, $ cat hash_a.rb #! /usr/local/bin/ruby # hash_a = Hash.new hash_a["bob"] = 1 hash_a["al"] = 2 hash_a["zac"] = 3 hash_a["moe"] = 4 puts "--- each" hash_a.each { |key, val| puts "k=#{key} v=#{val}" } puts "--- sort" hash_a.sort.each { |item| puts "k=#{item[0]} v=#{item[1]}" } $ ruby hash_a.rb --- each k=al v=2 k=moe v=4 k=zac v=3 k=bob v=1 --- sort k=al v=2 k=bob v=1 k=moe v=4 k=zac v=3 -- My apologies for the wasted bandwidth. -mark.