in reply to Re: Noodling with natural sorting in perl6
in thread Noodling with natural sorting in perl6
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 |