Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re: reverse sort arrays in subroutines

by davido (Archbishop)
on May 21, 2013 at 21:39 UTC ( #1034612=note: print w/replies, xml ) Need Help??

in reply to reverse sort arrays in subroutines

Are you trying to sort in descending order?

If so, you have a couple of choices. One, you can use a custom sort routine, eg:

my @SortedArray = sort { $b cmp $a } @PassedArray;

...or two, you can reverse the output of sort:

my @SortedArray = reverse sort @PassedArray;

The point I'm making is that reverse will act upon the sorted list. The way you have it, sort is acting upon a reversed list, which is incorrect. I hope I'm not missing your point. :) If that's not what you're asking, please clarify.

By the way, this isn't C++98 (vector.push_back()), you can populate your @TaR array like this: @TaR = ( 8, 2, 5, 4, 5, 6, 1, 8 ); ... and STDOUT is the default for print, so you don't usually need to explicitly mention it.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1034612]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2018-04-25 03:35 GMT
Find Nodes?
    Voting Booth?