Re: I need sorting help

[email protected] (Ralph Mellor)
Newsgroups perl.perl6.users
Message-ID <CAPLR5Sd8JOG0+LL2iD-TYvA_0Pq7suNYoOi4vxteXdjFLknucQ@mail.gmail.com>
On Sat, Mar 2, 2024 at 6:26 AM ToddAndMargo via perl6-users
<[email protected]> wrote:
>
> @Sorted_List = @Sorted_List.sort: { .comb(/ \d+ | \D+ /) .map({ .Int // .self })};

In case another answer is helpful...

First, a simplified sort that produces the same
result as your code above:

@Sorted_List .= sort: *.match: / \d+ /;

I can explain that in a later comment if you want, but it's
all stuff I recall you figuring out in the past, so for now I'll
just move on to address the change you wanted.

The problem you had was that sort defaults to a string sort.
In string sorting 1, 10, 2 are already sorted, but you instead
want numeric order, which sorts those three into 1, 2, 10.

To do that, coerce the sort value to numeric by inserting a `+`:

@Sorted_List .= sort: +*.match: / \d+ /;

--
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.