routine sort
Documentation for routine sort
assembled from the following types:
class Supply
From Supply
(Supply) method sort
method sort(Supply: ? --> Supply)
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
quit
s, then the exception is immediately conveyed on the return Supply
.
my = Supply.from-list(4, 10, 3, 2);my = .sort();.tap(); # OUTPUT: «23410»
class Any
From Any
(Any) method sort
Defined as:
multi method sort()multi method sort()
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().join; # OUTPUT: «cba»say '231'.comb.sort(:«<=>»).join; # OUTPUT: «123»
class Map
From Map
(Map) method sort
Defined as:
multi method sort(Map: --> Seq)
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.
class List
From List
(List) routine sort
Defined as:
multi sub sort(* --> Seq)multi sub sort(, * --> Seq)multi method sort(List: --> Seq)multi method sort(List: --> Seq)
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, -4, 7, -1, 2, 0).sort; # OUTPUT: «(-4 -1 0 2 3 7)»say (3, -4, 7, -1, 2, 0).sort: *.abs; # OUTPUT: «(0 -1 2 3 -4 7)»say (3, -4, 7, -1, 2, 0).sort: ; # 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 = (%( first-name => 'Kyle', last-name => 'Reese' ),%( first-name => 'Sarah', last-name => 'Connor' ),%( first-name => 'John', last-name => 'Connor' ),);.say for .sort: ;#`(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( );# 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( );# OUTPUT: «(01 02 2 11 20 111)»