From: Michal Rokos Date: 2002-01-06T16:35:25+09:00 Subject: [ruby-talk:30336] OT: Re: Sorting a Hash by value of integer stored in the Hash Hello, Yohanes Santoso wrote: > irb(main):011:0> myh = {"z"=>5,"x"=>3,"y"=>7} > irb(main):012:0> myh.sort{|a,b| a[1] <=> b[1]} > ary = [["x", 3], ["z", 5], ["y", 7]] ^^^^^^^ Is there any "automatic" way to create hash from ary (above). I didn't find any. (I know hash = Hash[[1,2,3,4]], but I doesn't work for this) Don't you think that it is strange is hash.to_a produces ary (above), but there is now way to create hash back from this ary. I think we should define new method class Hash def Hash::new_from_array(*ary) if ary.size == 1 then ary = ary[0] end hash = {} ary.each do |i| hash[i[0]] = i[1] end hash end end Or something like this, but with achieving compatibility with 'old' style Hash[]. Michal -- -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- Michal Rokos Czech Technical University, Prague E-mail: m.rokos@sh.cvut.cz Jabber: majkl@jabber.cz, ICQ: 36118339 -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-