Sort using a custom comparator: Difference between revisions

m
m (→‎{{header|Perl}}: sundry clean-up)
m (→‎{{header|Raku}}: simplified)
Line 3,132:
 
=={{header|Raku}}==
(formerly Perl 6)<br>
Primary sort by length of string, then break ties by sorting alphabetically (ignoring case).
<lang perl6>my @strings = <Here are some sample strings to be sorted>;
myput @sorted_strings = strings.sort :{ $^a.chars, <=> $^b.chars or $^a.lc cmp $^b.lc }, @strings;
say @sorted_strings =put sort -> $x { [ $x.chars, $x.lc ] }, @strings;</lang>
.say for @sorted_strings;
{{out}}
 
<pre>be to are Here some sample sorted strings
# If instead the function you feed to <code>sort</code> is of arity 1, it will do the Schwartzian transform for you, automatically sorting numeric fields numerically, and strings fields stringily:
be to are Here some sample sorted strings
 
</pre>
say @sorted_strings = sort -> $x { [ $x.chars, $x.lc ] }, @strings;</lang>
 
=={{header|REXX}}==
2,392

edits