On 2020-05-24 15:39, ToddAndMargo via perl6-users wrote:
On 2020-05-24 15:07, Tobias Boege wrote:
On Sun, 24 May 2020, Elizabeth Mattijsen wrote:
Hmmm... it appears we need to numerify the match to get numeric
comparison semantics, so we put a "+" before the match:
$ raku -e 'my @x=<a5 a2 a123 a133 a1>.sort: { +m/ \d+ $/ }; for @x {
say $_; }'
a1
a2
a5
a123
a133
So I think this would be a more general "version sort" then, where the
number
doesn't have to be at the end only:
@things.sort: {
.comb(/ \d+ | \D+ /)
.map({ .Int // .self })
}
It does not conform to all the rules about natural sort on Rosetta
Code [1]
but I like how
- longest-token matching,
- .sort behavior on (differently-sized) tuples, and
- .Int returning an (undefined) Failure
work together here.
Regards,
Tobias
[1] https://rosettacode.org/wiki/Natural_sorting
$ raku -e 'my @things = <a5 a2 a123 a133 a1 a22>.sort; dd @things; for
@things {say $_;}'
Array @things = ["a1", "a123", "a133", "a2", "a22", "a5"]
a1
a123
a133
a2
a22
a5
:'(
Ooops. Ignore that last comment
This is with what Tobias actually said
my @things = <a5 a2 a123 a133 a1 a22>.sort: { .comb(/ \d+ | \D+ /)
.map({ .Int // .self })}; for @things {say $_;}
a1
a2
a5
a22
a123
a133
my @things = <a5 a2 b123 a133 a1 a22>.sort: { .comb(/ \d+ | \D+ /)
.map({ .Int // .self })}; dd @things; for @things {say $_;}
Array @things = ["a1", "a2", "a5", "a22", "a133", "b123"]
a1
a2
a5
a22
a133
b123