In List§

See primary documentation in context for routine sort

multi        sort(*@elems      --> Seq:D)
multi        sort(&custom-routine-to-use*@elems --> Seq:D)
multi method sort(List:D:      --> Seq:D)
multi method sort(List:D: &custom-routine-to-use  --> Seq:D)

Sorts the list, smallest element first. By default infix:<cmp> is used for comparing list elements.

If &custom-routine-to-use is provided, and it accepts two arguments, it is invoked for pairs of list elements, and should return Order::Less, Order::Same or Order::More.

If &custom-routine-to-use accepts only one argument, the list elements are sorted according to custom-routine-to-use($a) cmp custom-routine-to-use($b). The return values of &custom-routine-to-use are cached, so that &custom-routine-to-use is only called once per list element.

Examples:

say (3-47-120).sort;                  # OUTPUT: «(-4 -1 0 2 3 7)␤» 
say (3-47-120).sort: *.abs;           # OUTPUT: «(0 -1 2 3 -4 7)␤» 
say (3-47-120).sort: { $^b leg $^a }# OUTPUT: «(7 3 2 0 -4 -1)␤»

Additionally, if &custom-routine-to-use returns a List, elements will be sorted based upon multiple values with subsequent values in the List being used to break the tie if the comparison between the prior elements evaluate to Order::Same.

my @resistance = (
    %first-name => 'Kyle',  last-name => 'Reese'  ),
    %first-name => 'Sarah'last-name => 'Connor' ),
    %first-name => 'John',  last-name => 'Connor' ),
);
.say for @resistance.sort: { .<last-name>.<first-name> };
 
#`(
OUTPUT:
  {first-name => John, last-name => Connor}
  {first-name => Sarah, last-name => Connor}
  {first-name => Kyle, last-name => Reese}
)

This sorting can be based on characteristics of a single element:

say <ddd aaa bbb bb ccc c>.sort{.chars.Str} );
# OUTPUT: «(c bb aaa bbb ccc ddd)␤» 

In this case, elements of the array are sorted in ascending order according first to the string length (.chars) and second to the actual alphabetical order .Str) if the length is exactly the same.

Any number of criteria can be used in this:

say <01 11 111 2 20 02>.sort{ .Int.comb.sum.Str } );
# OUTPUT: «(01 02 2 11 20 111)␤» 

Calling the sort sub without any arguments has become a runtime error as of release 2022.07 of the Rakudo compiler:

sort;   # ERROR: «Must specify something to sort␤» 

As of release 2023.08 of the Rakudo compiler it is also possible to specify a :k named argument. This will cause the result to be a list of indices of the sorting process.

say <a c b d e>.sort(:k); # OUTPUT: «(0 2 1 3 4)␤» 
say sort <a c b d e>:k# OUTPUT: «(0 2 1 3 4)␤»

In Supply§

See primary documentation in context for method sort

method sort(Supply:D: &custom-routine-to-use? --> Supply:D)

Taps the Supply it is called on. Once that Supply emits done, all of the values that it emitted will be sorted, and the results emitted on the returned Supply in the sorted order. Optionally accepts a comparator Block. If the original Supply quits, then the exception is immediately conveyed on the return Supply.

my $s = Supply.from-list(41032);
my $t = $s.sort();
$t.tap(&say);           # OUTPUT: «2␤3␤4␤10␤»

In Map§

See primary documentation in context for method sort

multi method sort(Map:D: --> Seq:D)

Returns a Seq of Pair objects, which are the pairs of the hash, sorted by key. Equivalent to %hash.sort: *.key

# These are equivalent: 
say Map.new(<c 3 a 1 b 2>).sort;        # OUTPUT: «(a => 1 b => 2 c => 3)␤» 
say Map.new(<c 3 a 1 b 2>).sort: *.key# OUTPUT: «(a => 1 b => 2 c => 3)␤»

See Any.sort for additional available candidates.

In Any§

See primary documentation in context for method sort

multi method sort()
multi method sort(&custom-routine-to-use)

Sorts iterables with cmp or given code object and returns a new Seq. Optionally, takes a Callable as a positional parameter, specifying how to sort.

Examples:

say <b c a>.sort;                           # OUTPUT: «(a b c)␤» 
say 'bca'.comb.sort.join;                   # OUTPUT: «abc␤» 
say 'bca'.comb.sort({$^b cmp $^a}).join;    # OUTPUT: «cba␤» 
say '231'.comb.sort(&infix:«<=>»).join;     # OUTPUT: «123␤» 
 
sub by-character-count { $^a.chars <=> $^b.chars }
say <Let us impart what we have seen tonight unto young Hamlet>.sort(&by-character-count);
# OUTPUT: «(us we Let what have seen unto young impart Hamlet tonight)␤»