Perl: sort arrays 

Joined:
04/09/2007
Posts:
710

September 25, 2008 23:55:31    Last update: September 25, 2008 23:55:31
# sort lexically
@articles = sort @files;

# same thing, but with explicit sort routine
@articles = sort {$a cmp $b} @files;

# sort numerically ascending
@articles = sort {$a <=> $b} @files;

# sort numerically descending
@articles = sort {$b <=> $a} @files;

# sort keys of hash numerically
foreach $key (sort { $hash{$b} <=> $hash{$a} } keys %hash) {
        printf "%4d %s\n", $hash{$key}, $key;
}

# this sorts the %age hash by value instead of key using an in-line function
@eldest = sort { $age{$b} <=> $age{$a} } keys %age;
Share |
| Comment  | Tags
 
Easy email testing with http://www.ximailstop.com