From: Adam Prescott Date: 2011-06-21T23:46:09+09:00 Subject: Re: How to order a hash based on its keys? On Tue, Jun 21, 2011 at 3:34 PM, Iñaki Baz Castillo wrote: > Hi, I want to order a hash using itds keys: > When you hear "order" and "hash" in the same sentence, you should think "hashes aren't ordered". >  irb> a = { 2=>"2", 3=>"3", 1=>"1" } >  irb> a.sort >  [[1, "1"], [2, "2"], [3, "3"]] > > But I want a resulting hash with keys ordered: > >  { 1=>"1", 2=>"2", 3=>"3"} > > Is there any efficiente way? (I don't want the hast to be converted to > an array and the to a hash again). > 1.9 keeps hashes initially in insertion order, but if you don't create your hash with the keys in the order you want, since Hash isn't ordered, I don't think there's a way to get what you want without creating an Array and then a second Hash, with the keys in the order you want.