Skip to content

Commit 26ab764

Browse files
Merge pull request #1619 from jstuder-gh/sort_multiple
Document List cmp infix and sorting by multiple values (via List)
2 parents e4cce1e + 191db0b commit 26ab764

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

CREDITS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ E: ilmari.vacklin@reaktor.fi
125125
N: isBEKaml
126126
E: nastavs@gmail.com
127127

128+
N: Jeremy Studer
129+
E: studerj1.mail@gmail.com
130+
128131
N: Jimmy Zhuo
129132
E: zhuomingliang@yahoo.com.cn
130133

doc/Type/List.pod6

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,7 @@ Defined as:
690690
multi method sort(List:D: --> Seq:D)
691691
multi method sort(List:D: &custom-routine-to-use --> Seq:D)
692692
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»
694694
is used for comparing list elements.
695695
696696
If C<&custom-routine-to-use> is provided, and it accepts two arguments,
@@ -709,6 +709,25 @@ Examples:
709709
say (3, -4, 7, -1, 2, 0).sort: *.abs; # OUTPUT: «(0 -1 2 3 -4 7)␤»
710710
say (3, -4, 7, -1, 2, 0).sort: { $^b leg $^a }; # OUTPUT: «(7 3 2 0 -4 -1)␤»
711711
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+
712731
=head2 routine unique
713732
714733
Defined as:
@@ -1220,6 +1239,31 @@ Assumes the C<List> contains L«C<Match> objects|/type/Match», such as the
12201239
C<$/> variable being a C<List>, when using C<:g> modifier in regexes. Returns the
12211240
value of C<.to> called on the last element of the list.
12221241
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+
12231267
=end pod
12241268

12251269
# vim: expandtab shiftwidth=4 ft=perl6

0 commit comments

Comments
 (0)