in reply to Re: Sort a 2D array based on 2 columns
in thread Sort a 2D array based on 2 columns
While this complicated sort function does work, if there is a lot of data, it is faster to precompute a sort key for each row, and sort using that. The Schwartzian Transform is the standard name for this, and you should read up on it. It is also in perlfaq4.
The general form of the ST is
@sorted = map { $_->[0] } sort { $a->[1] cmp $b->[1] } map { [$_, foo($_)] } @unsorted;
where foo() does whatever is necessary to make a sortable key. Obviously it is very specific to the data that you are working with. In your case the second column is an integer, and the fourth is either a 0 or a 1, so I'd go with something like
sub foo($) { $_[0]->[1] . "." . $_[0]->[3]; }
which will produce a single real number that you can compare using the <=> operator.
|
|---|