Sort using a custom comparator: Difference between revisions
Content added Content deleted
Alextretyak (talk | contribs) m (→{{header|11l}}) |
SqrtNegInf (talk | contribs) m (→{{header|Perl}}: sundry clean-up) |
||
Line 2,861: | Line 2,861: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
<lang perl>use feature 'say'; |
|||
{{works with|Perl|5.8.6}} |
|||
⚫ | |||
@strings = qw/Here are some sample strings to be sorted/; |
|||
my @sorted = sort mycmp @strings;</lang> |
|||
# with a subroutine: |
|||
⚫ | |||
⚫ | |||
<lang perl>my @strings = qw/here are some sample strings to be sorted/; |
|||
say join ' ', sort mycmp @strings; |
|||
⚫ | |||
Faster with a [[wp:Schwartzian transform|Schwartzian transform]]: |
|||
say join ' ', sort {length $b <=> length $a || lc $a cmp lc $b} @strings |
|||
<lang perl>my @strings = qw/here are some sample strings to be sorted/; |
|||
⚫ | |||
# for large inputs, can be faster with a 'Schwartzian' transform: |
|||
⚫ | |||
⚫ | |||
⚫ | |||
map { [ $_, length, lc ] } |
map { [ $_, length, lc ] } |
||
@strings;</lang> |
@strings;</lang> |
||
{{out}} |
|||
<pre>strings sample sorted Here some are be to |
|||
strings sample sorted Here some are be to |
|||
strings sample sorted Here some are be to</pre> |
|||
=={{header|Phix}}== |
=={{header|Phix}}== |