From: Vikhyat Korrapati Date: 2011-06-21T23:51:01+09:00 Subject: Re: How to order a hash based on its keys? --0016e6471bd4df848004a639fac2 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On Tue, Jun 21, 2011 at 8:04 PM, I=C3=B1aki Baz Castillo wr= ote: > Hi, I want to order a hash using itds keys: > > irb> a =3D { 2=3D>"2", 3=3D>"3", 1=3D>"1" } > irb> a.sort > [[1, "1"], [2, "2"], [3, "3"]] > > But I want a resulting hash with keys ordered: > > { 1=3D>"1", 2=3D>"2", 3=3D>"3"} > > Is there any efficiente way? (I don't want the hast to be converted to > an array and the to a hash again). > > Thanks. > > -- > I=C3=B1aki Baz Castillo > > > Atleast according to this StackOverflow question: http://stackoverflow.com/questions/4339553/sort-hash-by-key-return-hash-in-= ruby Hash[h.sort] seems to be the best way. Vikhyat Korrapati http://vikhyat.net/ --0016e6471bd4df848004a639fac2--