Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: reverse sort arrays in subroutines

by choroba (Abbot)
on May 21, 2013 at 21:47 UTC ( #1034615=note: print w/ replies, xml ) Need Help??


in reply to reverse sort arrays in subroutines

It is a misunderstanding. reverse is not a parameter to sort, it is a function that reverses a list.

Therefore,

sort reverse @array
is in fact
sort(reverse(@array))
i.e. it reverses the array and then sorts the result.
لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ


Comment on Re: reverse sort arrays in subroutines
Select or Download Code
Re^2: reverse sort arrays in subroutines
by JockoHelios (Scribe) on May 21, 2013 at 21:56 UTC

    davido, in my sample code, I'm trying to reverse the sort. I think I have your example but it only does the sort.

    : my @SortedArray = reverse sort @PassedArray;.

    in my sample code

    my @SortedArray = sort reverse @PassedArray;

    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 ) );
      I was probably not explicit enough. Let's take an example:
      1 3 2
      If we first reverse it, we get
      2 3 1
      and then we sort it to get
      1 2 3

      On the other hand, if we sort it first, we get

      1 2 3
      and we can then reverse it to
      3 2 1

      Functions are not called left to right, but from the innermost to the outermost, i.e. rather right to left.

      لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (14)
As of 2014-09-16 18:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (43 votes), past polls