Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

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

Replies are listed 'Best First'.
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?

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (12)
As of 2016-10-26 17:59 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (345 votes). Check out past polls.