----- Original Message -----
From: Matt Noel <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Wednesday, May 16, 2001 7:05 PM
Subject: Sorting a Two Dimensional Array


(...)> 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.  In other words, this
> arrangement:
>
>          Col #
>    R# |  0  1  2
> ------+----------
>    0  |  3  0  2
>    1  |  0  5  0
>    2  |  1  3  1
>    3  |  1  0  8
>
>

try making your own sort subroutine and pass it to sort:


my @out=([3,0,2],[0,5,0],[1,3,1],[1,0,8]);

my @sorted = sort {listcmp($a, $b)}@out ;

map{print "@$_\n"}@sorted;

sub listcmp {
   for (0..$#{$_[0]}) {return $_[0]->[$_] <=> $_[1]->[$_] || next}
 }

Reply via email to