Syntactic Confectionery Delight PerlMonks

### sort an array with +ve & -ve numbers in it

 on Apr 03, 2009 at 09:04 UTC Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks, I need to sort an array which is having positive & negative numbers.it should take these into account & give max number & min number. for example ,if my array has @array = (99, 67, 0, -100, -38, 98) then it should sort and give me -100 is min value & 99 is max value. currently i am using "sort" which is not working on the negative numbers.Please help! Thanks in advance.
• Comment on sort an array with +ve & -ve numbers in it

Replies are listed 'Best First'.
Re: sort an array with +ve & -ve numbers in it
by ikegami (Pope) on Apr 03, 2009 at 09:10 UTC
The default sort criteria sorts lexically (alphabetically). You need to specify your own sort criteria.
```my @sorted = sort { \$a <=> \$b } @numbers;
my \$min = \$sorted[0];
my \$max = \$sorted[-1];

An exposition of ikegami's reply (beginners in mind):

• my @sorted = sort { \$a <=> \$b } @numbers;
Calls sort on @numbers, storing returned list in @sorted. Passes sort a code block to use as a comparison function (used to determine whether to swap two elements). \$a and \$b can be used within the code block to reference the elements being compared. To reverse the sort order, reverse \$a and \$b in the code block:
sort { \$b <=> \$a } @numbers
• my \$min = \$sorted[0];
sort { \$a <=> \$b } will sort the numbers in ascending order, so the first element of the resultant array is the minimum.
• my \$max = \$sorted[-1];
An index of -1 refers to the last element, which is of course the maximum.

undef \$lunches{free}
Re: sort an array with +ve & -ve numbers in it
by jwkrahn (Monsignor) on Apr 03, 2009 at 10:51 UTC

You don't need to sort the array to get the minimum and maximum values, a simple loop will do:

```\$ perl -le'
my @array = ( 99, 67, 0, -100, -38, 98 );

my ( \$min, \$max ) = @array;

for my \$element ( @array ) {
\$min = \$element if \$min > \$element;
\$max = \$element if \$max < \$element;
}

print "Min = \$min and Max = \$max";
'
Min = -100 and Max = 99

Ummm...isn't a sort a bit easier and simpler?

```my @array = ( 99, 67, 0, -100, -38, 98);
my (\$min, \$max) = (sort @array)[0,-1];

___________
Eric Hodges
Easier and simpler, but less efficient (O n log n, perhaps) than jwkrahn's approach (O n). Of course this won't matter for small data sets.

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.

Re: sort an array with +ve & -ve numbers in it
by nagalenoj (Friar) on Apr 03, 2009 at 09:20 UTC
Re: sort an array with +ve & -ve numbers in it
by vinoth.ree (Monsignor) on Apr 03, 2009 at 10:01 UTC

You can also get the minimum value by using the

\$#sorted

This will hold the last element's index in the array

Re: sort an array with +ve & -ve numbers in it
by JavaFan (Canon) on Apr 03, 2009 at 09:24 UTC
i am using "sort" which is not working on the negative numbers.
Well, I'm a bit baffled by this. I guess what you are doing wrong - you're probably sorting alphabetically. But that doesn't work with positive numbers either.

Create A New User
Node Status?
node history
Node Type: perlquestion [id://755206]
Approved by linuxer
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2018-10-23 19:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
When I need money for a bigger acquisition, I usually ...

Results (125 votes). Check out past polls.

Notices?