Functional List Processing
Custom Sort
Sort small lists with a chosen direction.
Custom Sort
sort_custom.pl
use strict;
use warnings;
my $direction = ;
my @values = (3, 1, 2);
my @sorted;
if ($direction eq "asc") {
@sorted = sort { $a <=> $b } @values;
} else {
@sorted = sort { $b <=> $a } @values;
}
my $joined = join(",", @sorted);
print "direction=$direction\n";
print "sorted=$joined\n";
use strict;
use warnings;
my $direction = ;
my @values = (3, 1, 2);
my @sorted;
if ($direction eq "asc") {
@sorted = sort { $a <=> $b } @values;
} else {
@sorted = sort { $b <=> $a } @values;
}
my $joined = join(",", @sorted);
print "direction=$direction\n";
print "sorted=$joined\n";
custom-sort
Sort can compare values in the order a program needs. Numeric comparisons use the numeric comparison operator instead of string comparison.