sort() is a builtin PERL function with three modes: |
@result = sort @array; # equivalent to sort { $a cmp $b} @array; |
which sorts the variables in @array using stringwise comparisons, returning them in @result |
@result = sort BLOCK @array; # where statement BLOCK enclosed in {} curly brackets returns -1, 0, 1 given values of $a, $b |
@result = sort { $age{$a} <=> $age{$b} } @array; # sorts by age if entries in @arrays are keys to hash %age, which holds numeric age for each key |
@result = sort SUBNAME @array; # uses subroutine (which can be specified as value of scalar variable) to perform sorting |
sub backsort { $b <=> $a; } # Reverse order for integers |
@result = sort backsort @array; # sorts in numerically decreasing order |