Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- PERL 4.0 Reference Guide - Norton Guide http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]

     sort(SUBROUTINE LIST)

     sort(LIST)

     sort SUBROUTINE LIST

     sort LIST

             Sorts the LIST and returns the sorted  array  value.
             Nonexistent  values  of arrays are stripped out.  If
             SUBROUTINE is omitted, sorts in standard string com-
             parison  order.   If  SUBROUTINE is specified, gives
             the name of a subroutine  that  returns  an  integer
             less than, equal to, or greater than 0, depending on
             how the elements of the array  are  to  be  ordered.
             (The  <=>  and cmp operators are extremely useful in
             such routines.)  In the interests of efficiency  the
             normal  calling  code  for  subroutines is bypassed,
             with the following effects: the subroutine  may  not
             be  a  recursive subroutine, and the two elements to
             be compared are passed into the subroutine  not  via
             @_  but  as $a and $b (see example below).  They are
             passed by reference so don't modify $a and $b.  SUB-
             ROUTINE may be a scalar variable name, in which case
             the value provides the name  of  the  subroutine  to
             use.  Examples:

                  sub byage {
                      $age{$a} <=> $age{$b};    # presuming integers
                  }
                  @sortedclass = sort byage @class;

                  sub reverse { $b cmp $a; }
                  @harry = ('dog','cat','x','Cain','Abel');
                  @george = ('gone','chased','yz','Punished','Axed');
                  print sort @harry;
                       # prints AbelCaincatdogx
                  print sort reverse @harry;
                       # prints xdogcatCainAbel
                  print sort @george, 'to', @harry;
                       # prints AbelAxedCainPunishedcatchaseddoggonetoxyz

See Also: reverse

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson