On 5/5/07, dblack / wobblini.net <dblack / wobblini.net> wrote:
> Hi --
>
> On Sat, 5 May 2007, Nicholas Clare wrote:
>
> > On 5/5/07, Haoqi Haoqi <axgle / 126.com> wrote:
> >>
> >>
> >> > Hashes are unordered.  If you need an ordered collection, you'll need
> >> > to use an array.
> >>
> >>
> >> Hashes are unordered. that is how sadly for me,really!
> >>
> >> --
> >> Posted via http://www.ruby-forum.com/.
> >>
> >>
> >
> > Hi, I missed some of this thread, I only just joined the list. Although it's
> > less than optimal, can you not just sort the keys when you need them.
> > Something like:
> >
> > require 'enumerator'
> > sorted_keys = hash.enum_for(:each_key).to_a.sort
> >
> > Again, there might very well be a better way to do even that, I'm new to
> > ruby.
>
>
>    sorted_keys = hash.keys.sort
sorted_keys = hash.keys.sort_by{|k| k.to_s }
as pointed out already.


As an aside:
Will symbols be comparable in Ruby2? Am I off to yet another RCR, nooo!!!


But as another remark ordered Hashes do not necessarily mean "order by
keys" or "order by values" sometimes you want them ordered by
chronological order where
the hash literal assignment
hsh= {:a=>42, :b=>1764} would be equivalent ro
hsh={}
hsh[:a]=42
hsh[:b]=42*42
of course.


Cheers
Robert
>
> :-)
>
>
> David
>
> --
> Q. What is THE Ruby book for Rails developers?
> A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
>     (See what readers are saying!  http://www.rubypal.com/r4rrevs.pdf)
> Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
> A. Ruby Power and Light, LLC (http://www.rubypal.com)
>
>


-- 
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw