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

Re^3: reverse sort arrays in subroutines

by Laurent_R (Abbot)
on May 21, 2013 at 22:13 UTC ( #1034625=note: print w/replies, xml ) Need Help??

in reply to Re^2: reverse sort arrays in subroutines
in thread reverse sort arrays in subroutines

choroba, I tried your syntax in my code, but I still get only a sort - not a reverse sort. I'm sure this is part of my overall difficulties with arrays in subroutines, but I'm stymied. Here's what I tried: my @SortedArray = sort ( reverse ( @PassedArray ) );

You misunderstood Choroba's point.

Choroba was telling you that your syntax (sort reverse ...) is equivalent to sort (reverse (...)), and that, when you do that, you are first reversing the order of the array and then sorting it (so that, in fact, first reversing is is totally useless).

What you want to do is the other way around: you want to sort the array and then reverse the order of the array thus sorted, which should be done, as others have already pointed with the following syntax:

my @sorted_array = reverse sort @unsorted_array;

which is first sorting the array and then reversing the order of the elements (think of the data as moving right to left, from @unsorted_array, through sort, then through reverse, into @sorted_array).

EDIT: Choroba answered while I was typing. I guess it is clear by now.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1034625]
[ELISHEVA]: Simple yes. and I did consider that. but this isn't one off . An important data source that I don't control is generating bom prefixed utf8 files and I'd rather not have to be munging files every few months.
[erix]: on teh other hand a SOPW is pretty much garanteed to get an answer from tux (and probably the module fixed)
[ELISHEVA]: plus it bugs me that something that *should* be simple, *should* work- unicode and noms aren't exactly the new kids on the block
[ELISHEVA]: well then since the obvious possible mistakes on my part have been ruled out, SOPW it is.
[ELISHEVA]: the data source, or one of them, is the OECD - they provide a *lot* of data that ought to be easily available to perl programmers.
[erix]: it might be cunning to mention the module in the title... :)
[ELISHEVA]: fancy that - a title that actually describes the problem :-)
[ELISHEVA]: but actually thanks for the reminder
[Discipulus]: DBI::CSV + utf8 = BOO?M
[erix]: in extremis we tend to forget stuff ;)

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (7)
As of 2017-05-28 20:36 GMT
Find Nodes?
    Voting Booth?