Google


Perl Sort

sort SUBNAME LIST

sort BLOCK LIST

sort LIST

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). Note that in the latter case, it is usually counter-productive to declare $a and $b as lexicals. The values to be compared are always passed by reference and should not be modified.

Example 1: Sorting a list
    my @arr = ('Zebra', 'armadillo', 'Tiger', 'Antilop', 'Horse');
    say "@arr";
    Prints: Zebra armadillo Tiger Antilop Horse
    my @sorted = sort @arr;
    say "@sorted";
    Prints: Antilop Horse Tiger Zebra armadillo 

Example 2: Same as above with explicit sort block.

    my @arr = ('Zebra', 'armadillo', 'Tiger', 'Antilop', 'Horse');
    say "@arr";
    Prints: Zebra armadillo Tiger Antilop Horse
    my @sorted = sort { $a cmp $b } @arr;
    say "@sorted";
    Prints: Antilop Horse Tiger Zebra armadillo 

Example 3: Same as above, but now case-insensitively.

    my @arr = ('Zebra', 'armadillo', 'Tiger', 'Antilop', 'Horse');
    say "@arr";
    Prints: Zebra armadillo Tiger Antilop Horse
    my @sorted = sort { uc($a) cmp uc($b) } @arr;
    say "@sorted";
    Prints: Antilop armadillo Horse Tiger Zebra 

Example 4: Same as above, but now reversed order.

    my @arr = ('Zebra', 'armadillo', 'Tiger', 'Antilop', 'Horse');
    say "@arr";
    Prints: Zebra armadillo Tiger Antilop Horse
    my @sorted = sort { uc($b) cmp uc($a) } @arr;
    say "@sorted";
    Prints: Zebra Tiger Horse armadillo Antilop 

Example 5: Same as above, but now reversed order.

    my @arr = ('Zebra', 'armadillo', 'Tiger', 'Antilop', 'Horse');
    say "@arr";
    Prints: Zebra armadillo Tiger Antilop Horse
    my @sorted = sort { uc($b) cmp uc($a) } @arr;
    say "@sorted";
    Prints: Zebra Tiger Horse armadillo Antilop 

Example 6: Sort numerically ascending.

    my @arr = (9, 1, 3, 7, 2, 5, 4, 6, 8);
    say "@arr";
    Prints: 9 1 3 7 2 5 4 6 8
    my @sorted = sort { uc($a) cmp uc($b) } @arr;
    say "@sorted";
    Prints: 1 2 3 4 5 6 7 8 9 

Example 7: Sort numerically decending.

    my @arr = (9, 1, 3, 7, 2, 5, 4, 6, 8);
    say "@arr";
    Prints: 9 1 3 7 2 5 4 6 8
    my @sorted = sort { uc($b) cmp uc($a) } @arr;
    say "@sorted";
    Prints: 9 8 7 6 5 4 3 2 1 

www.oocgi.com.