@@ -690,7 +690,7 @@ Defined as:
690
690
multi method sort(List:D: --> Seq:D)
691
691
multi method sort(List:D: &custom-routine-to-use --> Seq:D)
692
692
693
- Sorts the list, smallest element first. By default C << infix:<cmp> >>
693
+ Sorts the list, smallest element first. By default L « C << infix:<cmp> >> |/routine/cmp »
694
694
is used for comparing list elements.
695
695
696
696
If C < &custom-routine-to-use > is provided, and it accepts two arguments,
@@ -709,6 +709,25 @@ Examples:
709
709
say (3, -4, 7, -1, 2, 0).sort: *.abs; # OUTPUT: «(0 -1 2 3 -4 7)»
710
710
say (3, -4, 7, -1, 2, 0).sort: { $^b leg $^a }; # OUTPUT: «(7 3 2 0 -4 -1)»
711
711
712
+ Additionally, if C < &custom-routine-to-use > returns a C < List > , elements will be
713
+ sorted based upon multiple values with subsequent values in the C < List > being
714
+ used to break the tie if the comparison between the prior elements evaluate to
715
+ C < Order::Same > .
716
+
717
+ my @resistance = (
718
+ %( first-name => 'Kyle', last-name => 'Reese' ),
719
+ %( first-name => 'Sarah', last-name => 'Connor' ),
720
+ %( first-name => 'John', last-name => 'Connor' ),
721
+ );
722
+ .say for @resistance.sort: { .<last-name>, .<first-name> };
723
+
724
+ #`(
725
+ OUTPUT:
726
+ {first-name => John, last-name => Connor}
727
+ {first-name => Sarah, last-name => Connor}
728
+ {first-name => Kyle, last-name => Reese}
729
+ )
730
+
712
731
= head2 routine unique
713
732
714
733
Defined as:
@@ -1220,6 +1239,31 @@ Assumes the C<List> contains L«C<Match> objects|/type/Match», such as the
1220
1239
C < $/ > variable being a C < List > , when using C < :g > modifier in regexes. Returns the
1221
1240
value of C < .to > called on the last element of the list.
1222
1241
1242
+ = head1 Operators
1243
+
1244
+ = head2 infix C « cmp »
1245
+
1246
+ multi sub infix:<cmp>(List @a, List @b)
1247
+
1248
+ Evaluates C < Lists > by comparing element C < @a[$i] > with C < @b[$i] > (for some
1249
+ C < Int $i > , beginning at 0) and returning C < Order::Less > , C < Order::Same > , or
1250
+ C < Order::More > depending on if and how the values differ. If the operation
1251
+ evaluates to C < Order::Same > , C < @a[$i + 1] > is compared with C < @b[$i + 1] > . This
1252
+ is repeated until one is greater than the other or all elements are exhausted.
1253
+
1254
+ If the C < Lists > are of different lengths, at most only C < $n > comparisons will be
1255
+ made (where C < $n = @a.elems min @b.elems > ). If all of those comparisons evaluate
1256
+ to C < Order::Same > , the final value is selected based upon which C < List > is
1257
+ longer.
1258
+
1259
+ say (1, 2, 3) cmp (1, 2, 3); # OUTPUT: «Same»
1260
+ say (4, 5, 6) cmp (4, 5, 7); # OUTPUT: «Less»
1261
+ say (7, 8, 9) cmp (7, 8, 8); # OUTPUT: «More»
1262
+
1263
+ say (1, 2) cmp (1, 2, 3); # OUTPUT: «Less»
1264
+ say (1, 2, 3) cmp (1, 2); # OUTPUT: «More»
1265
+ say (9).List cmp (^10).List; # OUTPUT: «More»
1266
+
1223
1267
= end pod
1224
1268
1225
1269
# vim: expandtab shiftwidth=4 ft=perl6
0 commit comments