On Tue, Jun 21, 2011 at 4:34 PM, I=F1aki Baz Castillo <ibc / aliax.net> wrote=
:
> Hi, I want to order a hash using itds keys:
>
> =A0irb> a =3D { 2=3D>"2", 3=3D>"3", 1=3D>"1" }
> =A0irb> a.sort
> =A0[[1, "1"], [2, "2"], [3, "3"]]
>
> But I want a resulting hash with keys ordered:
>
> =A0{ 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).

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

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/