On Wed, 15 Mar 2006, Pavel Smerk wrote:

> = how can I do 'perlish' a[1] <=> b[1] || a[2] <=> b[2] if I want compare a 
> and b accordind to some my own rules, i.e. if a[1] == b[2], "return" a[2] <=> 
> b[2]? In Ruby this is not possible, because 0 is true.

because in ruby you don't have too:

   a <=> b

when a and b are arrays just does that.  if you wanted to compare only the
first elements (assuming there are more) you can simply

   a[0,2] <=> b[0,2]

or, if you a glutten for perlishment

   [a[1], a[2]] <=> [b[0], b[1]]


you can do __really__ compact sorting routines in ruby.  i wrote one yesterday
that sorts strings like this

   /dmsp/nrt/data/incoming/afwa/2006.f13_0731556_DT.DAT
   /dmsp/nrt/data/incoming/afwa/2006.f13_0731737_DS.DAT

where

   2006.f13_0731556_DT.DAT
   ^^^^ ^^^ ^^^^^^^ ^^
   |    |   |       |
   year sat time    type

first by sat, then by year, then by time and finally by type

using only

   pats = %w[  f\d\d  ^\d{4}  _\d{7}  _\w{2}  ].map{|p|/#{p}/}

   basenames.sort!{|a,b| pats.map{|pat| a[pat]} <=> pats.map{|pat| b[pat]} }

gotta love that!!

cheers.

-a
-- 
share your knowledge.  it's a way to achieve immortality.
- h.h. the 14th dali lama