Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^4: sort an array with +ve & -ve numbers in it

by eric256 (Parson)
on Apr 03, 2009 at 15:58 UTC ( #755304=note: print w/ replies, xml ) Need Help??


in reply to Re^3: sort an array with +ve & -ve numbers in it
in thread sort an array with +ve & -ve numbers in it

Very true, corrected code:

my @array = ( 99, 67, 0, -100, -38, 98); my ($min, $max) = (sort {$a <=> $b} @array)[0,-1];

Update: Cut n paste error fixed, thanks plobsing and ikegami


___________
Eric Hodges


Comment on Re^4: sort an array with +ve & -ve numbers in it
Download Code
Re^5: sort an array with +ve & -ve numbers in it
by plobsing (Friar) on Apr 05, 2009 at 14:28 UTC

    Typo alert: you have 2 sorts. The result is that you sort alphabetically after having sorted numerically.

    Unfortunately, your test dataset doesn't show the problem. Try adding 100 to the data.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (9)
As of 2014-11-27 20:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (188 votes), past polls