Re: I need sorting help

[email protected] (yary)
Newsgroups perl.perl6.users
Message-ID <CAG2CFAaCPhBfsxwMuqe8o+wejNESYRVnH6FKHo+8kjjKFOBP2Q@mail.gmail.com>
Using my quick-intuition, are these methods sorting on the earliest number
withing the string, ignoring the non-digits?

$ raku -e '.say for <bk10 bk34 bk2 bk1>.sort: {m/ \d+ /.Int}'
$ raku -e '.say for <bk10 bk34 bk2 bk1>.sort: +*.match: / \d+ /'

let's see

$ raku -e '.say for <say31 what20 the30letters40 does2 not3 it1
matter10>.sort: {m/ \d+ /.Int}'
it1
does2
not3
matter10
what20
the30letters40
say31

What's been interesting about this thread is not only the variety of
solutions, also that the solutions are for two different questions-

how to sort by alpha & numeric segments (b3b, a1a, c20c) =>  (a1a, b3b, c2c)
how to sort by a numeric segment only (b3b, a1a, c20c) =>  (a1a, b3b, c20c)

-y


On Thu, Mar 7, 2024 at 4:40 PM Ralph Mellor <[email protected]> wrote:

> On Tue, Mar 5, 2024 at 7:01 AM ToddAndMargo via perl6-users
> <[email protected]> wrote:
>
> > >> $ raku -e '.say for <bk10 bk34 bk2 bk1>.sort(*.split(/\d+/,
> :kv).map({ (try .Numeric) // $_}).List)'
> > >>
> > >> Yippee!
>
> > > raku -e '.say for <bk10 bk34 bk2 bk1>.sort: { .comb(/ \d+ | \D+
> /).map({ .Int // .self }).cache };'
> >
> > Awesome!  Now I have two different methods!
>
> And now 4:
>
> $ raku -e '.say for <bk10 bk34 bk2 bk1>.sort: {m/ \d+ /.Int}'
> bk1
> bk2
> bk10
> bk34
>
> or, same logic, but spelled differently:
>
> $ raku -e '.say for <bk10 bk34 bk2 bk1>.sort: +*.match: / \d+ /'
> bk1
> bk2
> bk10
> bk34
>
> --
> love, raiph
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.