in reply to sorting multidimensional arrays
Note: I managed to screw up again by posting this in the wrong place. Two gaffes in a day. I hope the gods will forgive me.
If you seek a one-liner answer, I don't know what it is, but if you just want to get the job done, then simply transpose the matrix, apply the earlier solution (for ordering according to the n-th row elements), and then transpose back. I.e., for the general case of a non-square matrix,
sub transpose { map { my $j = $_; [ map $_[$_][$j], 0..$#_ ] } 0..$#{$_[0]}; } my @array = ([8, 9, 10], [6, 5, 4], [7, 8, 9]); my @sorted = transpose sort { $a->[1] <=> $b->[1] } transpose @array; 1; # target for c in DB __END__ DB<1> x @sorted 0 ARRAY(0x8359764) 0 10 1 9 2 8 1 ARRAY(0x84bdcb0) 0 4 1 5 2 6 2 ARRAY(0x84bdc8c) 0 9 1 8 2 7
the lowliest monk
|
|---|