-----Original Message-----
From: dblack / candle.superlink.net
To: ruby-talk / ruby-lang.org
Sent: 11/10/2002 11:04 AM
Subject: Re: Sorting an array of hashes

Hi --

One way would be:

  unsorted = (0...100).map {|t| { :k1 => rand(10), :k2 => t} }
  sorted = unsorted.sort do |a,b|
    if (n = a[:k1] <=> b[:k1]).zero?
      a[:k2] <=> b[:k2]
    else
      n
    end
  end

or, if you don't have my probably ridiculous aversion to the ternary
operator:

  unsorted = (0...100).map {|t| { :k1 => rand(10), :k2 => t} }
  sorted = unsorted.sort do |a,b|
    (n = a[:k1] <=> b[:k1]).zero? ? a[:k2] <=> b[:k2] : n
  end


David

-- 
David Alan Black                      | Register for RubyConf 2002!
home: dblack / candle.superlink.net     | November 1-3
work: blackdav / shu.edu                | Seattle, WA, USA
Web:  http://pirate.shu.edu/~blackdav | http://www.rubyconf.com

--------------------------------------------------

Hi David,

I'm not too averse to the ternary operator so I'll take your second version.

regards,

Martin