- or download this
sub natural_sort {
return @_ if @_ < 2;
my $index = 0;
return @_[@_».lc».subst(/(\d+)/, -> $/{ sprintf( "%s%c%s", 0, $0.c
+hars, $0) }, :g).map( *~' '~$index++ ).sort.map({@(split(' ',$^_))[*-
+1]})];
}
- or download this
sub natural_cmp ($a, $b) {
return [leg] ($a, $b)».lc».subst(/(\d+)/, -> $/{ sprintf( "%s%c%s",
+ 0, $0.chars, $0) }, :g);
}
- or download this
my %cache;
sub cached_natural_cmp ($a, $b) {
return [leg] ($a, $b)».lc.map( %cache{ * } //= *.subst(/(\d+)/, ->
+ $/{ sprintf( "%s%c%s", 0, $0.chars, $0) }, :g));
}
- or download this
@array.sort(*.lc.subst(/(\d+)/, -> $/{ sprintf( "%s%c%s", 0, $0.chars
+, $0) }, :g));
- or download this
@array.sort: natural(*);
sub natural ($) { *.lc.subst(/(\d+)/, -> $/{ sprintf( "%s%c%s", 0, $0.
+chars, $0) }, :g) }
- or download this
.say for %words.keys.sort: natural(*);
or
%words.keys.sort( natural(*) )».say;