sort LIST

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

��ŝ�鴻����潟�潟����㏍�鴻����с�壔��LIST �����純�若����������純�若�������������ŝ�鴻����ゃ��菴������障����� ��鴻�˨�í�潟�潟����㏍�鴻����с�壔��sort ��勖�壔����������壩�ɱ��臂í�с�����

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.

SUBNAME ��� BLOCK ��������ャ�����������sort ��壩��羣��� ���絖����罸�莠���勰����ŝ�ц����ŝ�������障����� SUBNAME ������絎���������������������壔����ŝ�鴻����勤��膣������í�勉�������ʒ����ŝ�˩研��鴻������� 綽������⓾��莢���勖�贋�違��0���罩c�勖�贋�違��菴������泣����˨�若����潟�勐�������с�������◑В�����������障����� (�����勉�������ŝ�˨�若����潟�˨�壔��<=> 羲�膊�絖���� cmp 羲�膊�絖������� ��������吾��箴水�í�с�����) SUBNAME ��壔����鴻�˨�ï����医��(羞糸����ŝ��)��с����������������勐�翫����˨�壔�������勐�ゃ��篏睡�������� 絎������勉�泣����˨�若����潟�勐�����(��障����壔����勉�ŝ����<�㋘�潟��)��◑В�����������障����� SUBNAME ��勌撮��������˨����≦����勉�ゃ�潟�í�ゃ�潟�純�若����˨�若����潟�������⓾��BLOCK ��� ��吾�������������с����障�����

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).

��泣����˨�若����潟�勉����㏍����帥�ゃ����� ($$)��勐�翫�����罸�莠�������荀�膣���奝��絽吾�勉�泣����˨�若����潟� ��������������� @_ ��勌賢��˨�ŝ����<�㋘�潟�鴻�������〠検��������障����� ��������壔����㏍����帥�ゃ����ŝ����勉�泣����˨�若����潟�������������с��; �����勐�翫����壩��莠���勉����� ��泣����˨�若����潟��羝<��������篋���ゃ�勤��膣���壔����������宴�若�吾�勉�違�㏍�若�����紊���� $a �� $b ��ф検��������障��(罨<�勌����������с����⓾�����������)���

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.

��泣����˨�若����潟�� XSUB ��勐�翫�����罸�莠����������荀�膣���壔����勰�����綣���違�� XSUB ���羝<��綵≪�с�� ��鴻�帥����壔�˨�������激�ャ�������障����� $a ��� $b ��虁┃絎���������障��������

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.

��障�������純�若�������㏍����壔����泣����˨�若����潟����� perlsyn ��цą�����������⓾����� ��˨�若����九勝絖���� goto ���篏帥�c�〠�������⓾�壔�������障��������

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

use locale ���������(��������� use locale ':not_characters' ��� �����鴻�с�ŝ��)��勐�翫�����sort LIST ��� LIST ����憜����勖��莠���㏍�宴�若�˨��緇���c� ��純�若�������障����� 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.

sort ��壠����勉�ŝ�鴻����吾�勉����ゃ�ŝ�≪�鴻��菴������障��; for ��˨�若����勉�ゃ�潟�������壔�劫����違����ŝ�鴻��荀�膣���吾�勉����ゃ�ŝ�≪�鴻�����罕���с����� ��ゃ�障�����sort ��ц�������������ŝ�鴻����勤��膣����(箴������違�� foreach ��� map ��� grep ���)紊���眼�����������絎������������勉�ŝ�鴻����勤��膣���� 紊���眼�������障����� ��������壔����������ŝ�潟�若�������吾����������˨�壩�勰����壠����帥�������障�����

Historically Perl has varied in whether sorting is stable by default. If stability matters, it can be controlled explicitly by using the sort pragma.

罩翫兏����˨�壔����純�若�������������í�˨����у��絎������í�������壩�������с����� 絎�絎���с����馹���˨�ŝ����翫����壔��sort �����í�違�����篏帥����������ф��腓榊�������九勝��с����障�����



    # sort lexically
    my @articles = sort @files;

    # same thing, but with explicit sort routine
    my @articles = sort {$a cmp $b} @files;

    # now case-insensitively
    my @articles = sort {fc($a) cmp fc($b)} @files;

    # same thing in reversed order
    my @articles = sort {$b cmp $a} @files;

    # sort numerically ascending
    my @articles = sort {$a <=> $b} @files;

    # sort numerically descending
    my @articles = sort {$b <=> $a} @files;

    # this sorts the %age hash by value instead of key
    # using an in-line function
    my @eldest = sort { $age{$b} <=> $age{$a} } keys %age;

    # sort using explicit subroutine name
    sub byage {
        $age{$a} <=> $age{$b};  # presuming numeric
    my @sortedclass = sort byage @class;

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

    # inefficiently sort by descending numeric compare using
    # the first integer after the first = sign, or the
    # whole record case-insensitively otherwise

    my @new = sort {
        ($b =~ /=(\d+)/)[0] <=> ($a =~ /=(\d+)/)[0]
                    fc($a)  cmp  fc($b)
    } @old;

    # same thing, but much more efficiently;
    # we'll build auxiliary indices instead
    # for speed
    my (@nums, @caps);
    for (@old) {
        push @nums, ( /=(\d+)/ ? $1 : undef );
        push @caps, fc($_);

    my @new = @old[ sort {
                           $nums[$b] <=> $nums[$a]
                           $caps[$a] cmp $caps[$b]
                         } 0..$#old

    # same thing, but without any temps
    my @new = map { $_->[0] }
           sort { $b->[1] <=> $a->[1]
                  $a->[2] cmp $b->[2]
           } map { [$_, /=(\d+)/, fc($_)] } @old;

    # using a prototype allows you to use any comparison subroutine
    # as a sort subroutine (including other package's subroutines)
    package Other;
    sub backwards ($$) { $_[1] cmp $_[0]; }  # $a and $b are
                                             # not set here
    package main;
    my @new = sort Other::backwards @old;

    # guarantee stability
    use sort 'stable';
    my @new = sort { substr($a, 3, 5) cmp substr($b, 3, 5) } @old;

Warning: syntactical care is required when sorting the list returned from a function. If you want to sort the list returned by the function call find_records(@key), you can use:

茘☗��: ��∽�違�����菴������������ŝ�鴻�������純�若����������������˨�壩��羈�筝���勖絵������綽�荀���с����� ��∽�医�若�喝�冴�� find_records(@key) ������菴������������ŝ�鴻�������純�若�������������翫����� 篁ヤ����勉����������堺�ャ�障��:

    my @contact = sort { $a cmp $b } find_records @key;
    my @contact = sort +find_records(@key);
    my @contact = sort &find_records(@key);
    my @contact = sort(find_records(@key));

If instead you want to sort the array @key with the comparison routine find_records() then you can use:

筝���鴻�������� @key ���罸�莠���˨�若����� find_records() ��с�純�若�������������翫����壔�� 篁ヤ����勉����������堺�ャ�障��:

    my @contact = sort { find_records() } @key;
    my @contact = sort find_records(@key);
    my @contact = sort(find_records @key);
    my @contact = sort(find_records (@key));

$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 ��� $b ��壔��sort() �����若�喝�冴�������������宴�若�吾�勉�������宴�若�吾�違�㏍�若����˨������� 荐㊤����������障����� ��ゃ�障�����main ��������宴�若�吾�� $main::a ��� $main::b (������������ $::a ��� $::b) ��� FooPack ��������宴�若�吾�� $FooPack::a ��� $FooPack::b�����ŝ�í�с����� ��純�若�������㏍����壔�� $a ��� $b ��� my ��障����� state ��勉�鴻�潟�若�������勐�翫����� ��純�若�������㏍����壔�勐����違�勐����������� ���絎������ŝ�������違�ŝ����障�����:

   package main;
   my $a = "C"; # DANGER, Will Robinson, DANGER !!!

   print sort { $a cmp $b }               qw(A C E G B D F H);
                                          # WRONG
   sub badlexi { $a cmp $b }
   print sort badlexi                     qw(A C E G B D F H);
                                          # WRONG
   # the above prints BACFEDGH or some other incorrect ordering

   print sort { $::a cmp $::b }           qw(A C E G B D F H);
                                          # OK
   print sort { our $a cmp our $b }       qw(A C E G B D F H);
                                          # also OK
   print sort { our ($a, $b); $a cmp $b } qw(A C E G B D F H);
                                          # also OK
   sub lexi { our $a cmp our $b }
   print sort lexi                        qw(A C E G B D F H);
                                          # also OK
   # the above print ABCDEFGH

With proper care you may mix package and my (or state) $a and/or $b:

��ï�����羈������������違����������宴�若�吾�� my (������������ state) $a ��� $b ��� 羞激����������������с����障��:

   my $a = {
      tiny   => -2,
      small  => -1,
      normal => 0,
      big    => 1,
      huge   => 2

   say sort { $a->{our $a} <=> $a->{our $b} }
       qw{ huge normal tiny small big};

   # prints tinysmallnormalbighuge

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

$a ��� $b ��� 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 :

$a ��� $b ���篏帥�c�〠�吾����������純�若����泣����˨�若����潟�壔����勐�若�喝�冴����������宴�若�吾� �����ŝ�������違�ŝ����障�������� ��違�ŝ����������宴�若�吾��絎�臂í�������������壠櫝�純�с�������� ��������壠櫝�純�с����������������������√����������������障�����; ��泣����˨�若����潟�壠�若�喝�冴����������宴�若�吾�� $a ��� $b ��� �����с����ŝ�������違�ŝ����ŝ����������с��:

   package Foo;
   sub lexi { $Bar::a cmp $Bar::b }
   package Bar;
   ... sort Foo::lexi ...

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.

罸�莠���∽�違�壚��莢˨�������壔��������������������������羆������������障����� 筝�莢˨����ŝ��腟�������菴����(箴������違����������������� $x[1] ��� $x[2] ������ 絨���������◑����������障����������������奝�����菴����)��翫�����腟������壩�ɱ��臂í�с�����

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 NaNs from the input list.

<=> ��壔�í�<�������勉�ŝ����í�潟����� NaN (not-a-number) ��勉������� undef ���菴������勉�с��$a <=> $b ��������c��罸�莠���∽�違� ��純�若����������翫����壔�ŝ�鴻����� NaN �����˨�障����ŝ�����������羈���������⓾�������������� 篁ヤ����勌����� ��ュ����ŝ�鴻�������� NaN �����������ゃ����������� NaN != NaN �����������ц蟹��� ��ñ�������⓾����障�����

    my @result = sort { $a <=> $b } grep { $_ == $_ } @input;

In this version of perl, the sort function is implemented via the mergesort algorithm.

�����勉����若�吾�с�潟�� perl ��с�壔��sort ��∽�違�壔����若�吾�純�若����≪�˨�眼�ŝ�冴���� 絎�茖���������⓾����障�����