I am looking for some better methods like sort_on_fields(1,2,4,6,9) rather than sort { $a1 <=> $b1 or .. }
Assuming that you mean that the sort field numbers cannot be hard coded as in the FAQ, then something like this does the trick:
#! perl -slw
use strict;
use Data::Dump qw[ pp ];
sub sortOnFields{
my $r;
$r = $a->[ $_ ] <=> $b->[ $_ ] and return $r for @_;
return 0;
}
my @data = map[
map int( rand 10 ), 1 .. 10
], 1 .. 20;
@data = sort{ sortOnFields( 0,2,4,6 ) } @data;
pp \@data;
__END__
C:\test>908993
[
[0, 9, 1, 9, 0, 8, 7, 5, 9, 5],
[0, 2, 9, 7, 5, 9, 1, 8, 8, 7],
[1, 0, 2, 4, 1, 8, 3, 4, 6, 4],
[1, 6, 6, 7, 6, 0, 3, 1, 8, 8],
[1, 8, 8, 0, 4, 5, 0, 8, 8, 3],
[2, 5, 7, 6, 5, 4, 6, 8, 2, 0],
[3, 2, 0, 4, 2, 5, 1, 1, 6, 6],
[3, 2, 1, 2, 2, 4, 7, 9, 8, 5],
[3, 1, 6, 9, 0, 3, 0, 5, 6, 4],
[5, 5, 3, 8, 1, 1, 0, 5, 7, 0],
[5, 1, 5, 7, 9, 8, 3, 3, 8, 7],
[5, 6, 6, 4, 2, 9, 7, 7, 2, 4],
[6, 9, 1, 6, 5, 5, 0, 4, 0, 1],
[9, 3, 0, 3, 0, 1, 2, 2, 7, 0],
[9, 2, 0, 6, 8, 6, 0, 6, 5, 0],
[9, 7, 0, 3, 8, 2, 2, 3, 7, 0],
[9, 1, 2, 7, 2, 9, 6, 7, 3, 9],
[9, 5, 2, 0, 2, 4, 8, 0, 7, 1],
[9, 9, 2, 2, 8, 5, 8, 3, 3, 0],
[9, 1, 4, 4, 5, 0, 2, 6, 8, 8],
]
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
|