From: Robert Klemme Date: 2011-06-22T00:24:22+09:00 Subject: Re: How to order a hash based on its keys? On Tue, Jun 21, 2011 at 4:34 PM, Iņaki Baz Castillo wrote: > Hi, I want to order a hash using itds keys: > >  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). See Adam's and Josh's replies: a Hash is generally unordered. However, what do you need this for? If it is for debugging purposes then you might as well override #inspect on a per instance basis or change it in Hash (not recommended). If you need that for other reasons then maybe a tree might be a better choice. There is for example http://raa.ruby-lang.org/project/ruby-rbtree/ Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/