Documentation for routine sort

Documentation for routine sort, assembled from the following types:

class Any

From Any

(Any) method sort

Defined as:

multi method sort()
multi method sort(&by)

Sorts iterables with infix:<cmp> or given code object and returns a new List. 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␤» 

class List

From List

(List) routine sort

Defined as:

multi sub    sort(*@elems      --> Seq:D)
multi sub    sort(&by*@elems --> Seq:D)
multi method sort(List:D:      --> Seq:D)
multi method sort(List:D: &by  --> Seq:D)

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

If &by 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 &by accepts only one argument, the list elements are sorted according to by($a) cmp by($b) . The return values of &by are cached, so that &by 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)␤» 

class Supply

From Supply

(Supply) method sort

method sort(Supply:D: &by? --> Supply:D)

Waits until the given supply is done, then creates a supply to which all values seen are emitted in sorted order. Optionally accepts a comparator Block.