On Wed, May 16, 2001 at 10:05:14AM -0700, Matt Noel wrote:
> I have a simple two-dimensional array, call it @Weights.  I think of the
> first index as being the Row index and the second being the Column
> index.  Normally I'd access an entry thus:
> 
>   $ItemWeight = $Weights[$row][$col];
> 
> I just want to sort the rows first by the first column, then by the
> second column, etc for all the columns.

You'll be wanting to use the spaceship operator <=>

The key to the solution, is that if the values being compared are the
same, the operator will return 0, and you can then use the
short-circuiting || operator to only check the next column if necessary.

So, something like:

my @sorted = sort {
                      $a->[0] <=> $b->[0]
                              ||
                      $a->[1] <=> $b->[1]
                              ||
                      $a->[2] <=> $b->[2]
                  } @Weights;

If you have many more columns you'll probably want to put some sort of
loop in the sort function.

-- 
Paul Johnson - [EMAIL PROTECTED]
http://www.pjcj.net

Reply via email to