Sort using a custom comparator: Difference between revisions

Content added Content deleted
m (→‎{{header|Perl}}: sundry clean-up)
m (→‎{{header|Raku}}: simplified)
Line 3,132: Line 3,132:


=={{header|Raku}}==
=={{header|Raku}}==
(formerly Perl 6)
(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>;
<lang perl6>my @strings = <Here are some sample strings to be sorted>;
my @sorted_strings = sort { $^a.chars <=> $^b.chars or $^a.lc cmp $^b.lc }, @strings;
put @strings.sort:{.chars, .lc};
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}}==
=={{header|REXX}}==