From: Harry Kakueki Date: 2012-11-30T16:09:59+09:00 Subject: Re: Ordered Hash (v. 1.8) > I am running Ruby v. 1.8.7 and I have installed the orderedhash gem. I > know that the purpose of orderedhash is to preserve the order of > creation. However, what I'd really like to do is, after the hash has > been created, sort it on the key in a manner similar to the way that I > would sort an array with the sort!() method and then preserve the sorted > order. Any ideas on how I might accomplish that feat? Thanks for any > input. > > ... doug # If you want to also keep the original order, maybe this is worth a look. # I'm using 1.9 so I can't test it for your setup. class Hash def myway Hash[*sort.flatten] #Ruby 1.8 ? #Hash[sort] # Ruby1.9 end end h = {} arr = ["t","c","h","b","l"] arr.each{|x| h[x] = x + "ook"} p h #> {"t"=>"took", "c"=>"cook", "h"=>"hook", "b"=>"book", "l"=>"look"} p h.myway #> {"b"=>"book", "c"=>"cook", "h"=>"hook", "l"=>"look", "t"=>"took"} Harry