Monday, March 03, 2003

CodeBit; Sorting Arrays With Numbers Using Perl

If you are looking to use the sort method to sort an array which contains numbers, the regular sort @array technique won't do you much good.

To sort an array, do the following:

# from least to greatest
foreach$number(sort { $a <=> $b } @numbers) {
   print $number\n";
}
# from greatest to least:
foreach$number(sort { $b <=> $a } @numbers) {
   print $number\n";
}
Post a Comment