sort

In list context, this sorts the LIST and returns the sorted list value.
In scalar context, the behaviour of sort is
undefined.

If SUBNAME or BLOCK is omitted, sorts in
standard string comparison
order. If SUBNAME is specified, it 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 list are to be ordered. (The
<=>
and cmp
operators are extremely useful in such routines.)
SUBNAME may be a scalar variable name (unsubscripted), in which case
the value provides the name of (or a reference to) the actual
subroutine to use. In place of a SUBNAME, you can provide a BLOCK as
an anonymous, in-line sort subroutine.

If the subroutine's prototype is ($$)
, the elements to be compared are
passed by reference in @_ , as for a normal subroutine.
This is slower than unprototyped subroutines, where the elements to be
compared are passed into the subroutine as the package global variables
$a
and $b
(see example below).

If the subroutine is an XSUB, the elements to be compared are pushed on
to the stack, the way arguments are usually passed to XSUBs. $a
and
$b
are not set.

The values to be compared are always passed by reference and should not
be modified.

You also cannot exit out of the sort block or subroutine using any of the
loop control operators described in perlsyn or with
goto.

When use locale (but not uselocale':not_characters'
)
is in effect, sortLIST
sorts LIST according to the
current collation locale. See perllocale.

sort returns aliases into the original list,
much as a for loop's index variable aliases the list elements. That is,
modifying an element of a list returned by sort
(for example, in a foreach
, map or
grep)
actually modifies the element in the original list. This is usually
something to be avoided when writing clear code.

Perl 5.6 and earlier used a quicksort algorithm to implement sort.
That algorithm was not stable and could go quadratic. (A stable sort
preserves the input order of elements that compare equal. Although
quicksort's run time is O(NlogN) when averaged over all arrays of
length N, the time can be O(N**2), quadratic behavior, for some
inputs.) In 5.7, the quicksort implementation was replaced with
a stable mergesort algorithm whose worst-case behavior is O(NlogN).
But benchmarks indicated that for some inputs, on some platforms,
the original quicksort was faster. 5.8 has a sort pragma for
limited control of the sort. Its rather blunt control of the
underlying algorithm may not persist into future Perls, but the
ability to characterize the input or output in implementation
independent ways quite probably will.

$a
and $b
are set as package globals in the package the sort() is
called from. That means $main::a
and $main::b
(or $::a
and
$::b
) in the main
package, $FooPack::a
and $FooPack::b
in the
FooPack
package, etc. If the sort block is in scope of a my or
state declaration of $a
and/or $b
, you must spell out the full
name of the variables in the sort block :

$a
and $b
are implicitely local to the sort() execution and regain their
former values upon completing the sort.

Sort subroutines written using $a
and $b
are bound to their calling
package. It is possible, but of limited interest, to define them in a
different package, since the subroutine must still refer to the calling
package's $a
and $b
:

Use the prototyped versions (see above) for a more generic alternative.

The comparison function is required to behave. If it returns
inconsistent results (sometimes saying $x[1]
is less than $x[2]
and
sometimes saying the opposite, for example) the results are not
well-defined.

Because <=>
returns undef when either operand
is NaN
(not-a-number), be careful when sorting with a
comparison function like $a <=> $b
any lists that might contain a
NaN
. The following example takes advantage that NaN != NaN
to
eliminate any NaN
s from the input list.