Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Reverse Alphabetically Sorting an array

by matra555 (Monk)
on Jan 09, 2005 at 23:17 UTC ( [id://420765]=CUFP: print w/replies, xml ) Need Help??

It's easy enough to do:

my (@array) = ("fred","barney","wilma","betty"); @array = sort @array; @array = reverse @array; print "@array\n";

But if you have to do that with a lot of arrays in one program, you can save on typing by putting it into a sub:

sub reversi { my (@array) = @_; print reverse sort(@array); }

and then just call reversi(@yourarrayhere)

UPDATE: Modified sub code to follow jZed's suggestion. UPDATE: Fixed spelling error pointed out.

Replies are listed 'Best First'.
•Re: Reverse Alphabetically Sorting an array
by merlyn (Sage) on Jan 10, 2005 at 01:02 UTC
    I'm not sure why I'd introduce the overhead of calling a subroutine, when "reverse sort" is a natural idiom (it reads like what it does).

    Can you motivate your post a little better for me?

    Also, keep in mind that recent Perls actually note the "reverse sort" combo, and never bother creating the forward list just to reverse it: instead, "sort" sorts backwards (how clever is that?).

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      I could be mistaken, but if I was maintaining someone elses code, it would save me typing to write a sub and plug the arrays into it, rather than retype them to fit the idiom.

      Moreover, I did check the relevant categories in Q&A, and didn't see it there.

        I don't like adding functions for such overly trivial things. It doesn't take someone else any more effort to read reverse sort @foo than to read reversi @foo, but with the cute name you used for the latter, they'll have to look up what the function does in the first place.

        Makeshifts last the longest.

        It's not in there because it's obvious. :)

        --
        mowgli

Re: Reverse Alphabetically Sorting an array
by jZed (Prior) on Jan 09, 2005 at 23:24 UTC
    What's wrong with:

    print reverse sort qw(fred barney wilma betty);

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Reverse Alphabetically Sorting an array
by DaWolf (Curate) on Jan 11, 2005 at 05:30 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://420765]
Approved by jfroebe
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (2)
As of 2024-04-26 07:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found