Re: I need sorting help
[email protected] (ToddAndMargo via perl6-users)
| Newsgroups | perl.perl6.users |
|---|---|
| Message-ID | <[email protected]> |
On 3/4/24 13:55, ToddAndMargo via perl6-users wrote: > On 3/4/24 12:40, Ralph Mellor wrote: >> 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 > > > $ raku -e '.say for <bk10 bk34 bk2 bk1>.sort(*.split(/\d+/, :kv).map({ > (try .Numeric) // $_}).List)' > bk1 > bk2 > bk10 > bk34 > > Yippee! > > > tony@rn6:/home/linuxutil1$ raku -e '.say for <bk10 bk34 bk2 bk1>.sort: { > .comb(/ \d+ | \D+ /) .map({ .Int // .self })};' > bk1 > bk10 > bk2 > bk34 > > Rats! Thank for trying anyway! > $ raku -e '.say for <bk10 bk34 bk2 bk1>.sort: { .comb(/ \d+ | \D+ /) .map({ .Str // .self })};' bk1 bk10 bk2 bk34 Double Rats!