http://www.perlmonks.org?node_id=2256

vroom has asked for the wisdom of the Perl Monks concerning the following question:

How do I sort a list in reverse order?

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How do I sort a list in reverse order?
by vroom (His Eminence) on Jan 21, 2000 at 00:36 UTC
    Use a sort block and switch the positions of the special variable $a and $b
    @sorted=sort{$b cmp $a} @unsorted; # for strings @sorted=sort($b <=> $a) @unsorted; # for numbers
    Another option is:
    @sorted=reverse sort @unsorted #for strings
Re: How do I sort a list in reverse order?
by denisvaldenaire (Initiate) on Apr 23, 2018 at 13:59 UTC
    Note that

    @sorted=sort($b <=> $a) @sorted;   # for number

    won't work, should be

    @sorted=sort{$b <=> $a} @unsorted;

    Originally posted as a Categorized Answer.

Re: How do I sort a list in reverse order?
by St{)ìÑ (Initiate) on Oct 19, 2002 at 18:58 UTC
    The best way is to use a reverse structure on a sorted list or to reverse sort a list. This is in Chapter 3 of Learning Perl by O'REILLY: reverse.

    <STDIN>

    Originally posted as a Categorized Answer.