in reply to Re: Noodling with natural sorting in perl6
in thread Noodling with natural sorting in perl6

rx/ [\w+] ** <punct> /

Cool, thanks. I am still stuck in perl5 regex think to a large extent.

sub natural_cmp ($a, $b) { my ($one, $two) = ($a, $b).map(*.subst(/(\d+)/, -> $/{ sprintf( "%s%c%s", 0, $0.chars, $0) }, :g).lc); return $one cmp $two; }

Ooooo shiney! Like I said, I can be dense sometimes. Even better, do away with all the intermediate variables.

sub natural_cmp ($a is copy, $b is copy) { return $a.subst(/(\d+)/, -> $/{ sprintf( "%s%c%s", 0, $0.chars, $0) + }, :g).lc cmp $b.subst(/(\d+)/, -> $/{ sprintf( "%s%c%s", 0, $0.chars, $0) }, + :g).lc; }

Replies are listed 'Best First'.
Re^3: Noodling with natural sorting in perl6
by moritz (Cardinal) on Aug 20, 2010 at 20:26 UTC
    That way you duplicated the transformation code.

    Here's a version that uses neither temporaries nor duplicated code:

    sub natural_cmp ($a is copy, $b is copy) { return [cmp] ($a, ).map: *.subst(/(\d+)/, -> $/{ sprintf( "%s%c%s", + 0, $0.chars, $0) }, :g).lc }

    This uses the reduction meta operator.

    Perl 6 - links to (nearly) everything that is Perl 6.