http://www.perlmonks.org?node_id=2252

vroom has asked for the wisdom of the Perl Monks concerning the following question: (sorting)

How do I sort a list of numbers?

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How do I sort a list of numbers?
by vroom (His Eminence) on Jan 21, 2000 at 00:15 UTC
    you need to use the numerical comparison operator <=> inside of your sort function
    @sorted=sort{$a <=> $b} @unsorted;
    If you did
    @sorted=sort @unsorted;
    the sort function defaults to doing a textual comparison so 375 would be considered less than 4 since its first character 3 is less than 4.
Re: How do I sort a list of numbers?
by carrolte (Novice) on Dec 12, 2000 at 21:42 UTC
    Here is a twist: How to sort an array of referenced hashes, based on a perticular key value.

    @sorted=sort{$$a{key} <=> $$b{key}} @unsorted;

    carrolte
Re: How do I sort a list of numbers?
by Anonymous Monk on Jun 03, 2002 at 05:33 UTC

    Re: How do I sort a list of numbers?

    Originally posted as a Categorized Answer.

Re: How do I sort a list of numbers?
by St{)ìÑ (Initiate) on Oct 19, 2002 at 18:43 UTC
    Here is my answer, You need to use the sort command structure. Like this...

    @sortedlist = sort @unsortedlist That will sort your list in  ASCIIbetical order. ASCII is a strange place where Caps come before lower case letters and punctuation marks-well they come in all different places. This is the default behavior of sort. You can change this order to whatever you prefer.

    <STDIN>

    Originally posted as a Categorized Answer.