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

As already stated by ikegami, you really need to use the numerical sort: sort {$a <=> $b} @array

Try changing your data set to my @array = ( -10, 99, 67, 0, -100, -38, 98) and using that sort routine. This is probably the problem the OP is having. You're going to get -10 as the minimum.

Replies are listed 'Best First'.
Re^4: sort an array with +ve & -ve numbers in it
by eric256 (Parson) on Apr 03, 2009 at 15:58 UTC

    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

      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.