Gossamer Forum
Home : General : Perl Programming :

Sort array from Large to Small

Quote Reply
Sort array from Large to Small

I am trying to print out a list of numbers in order from largest to smallest and all the numbers would be stored in an array. Is there any way to do that? Thanks for the help.


Last edited by:

snowdude46: Jan 23, 2004, 9:52 PM
Quote Reply
Re: [snowdude46] Sort array from Large to Small In reply to
Use the sort{} function, but instead of $a <=> $b (or nothing) in the brackets, you want $b <=> $a.

print $_, for (sort { $b <=> $a} (1 .. 10));

Note that non-numeric values should be compared with "cmp", multiple rules can be used if seperated by "||" or "or", manipulate the values of the two special variables before they are compared, or even write your own custom sorting routine to be used in conjuction with sort.

Limecat is not pleased.