Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

How do I sort a list of numbers?

by vroom (His Eminence)
on Jan 21, 2000 at 00:12 UTC ( [id://2252]=perlquestion: print w/replies, xml ) Need Help??

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://2252]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (7)
As of 2024-03-19 02:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found