my %cache; sub pre_process($word) { unless %cache.exists($word) { %cache{$word} = $word.subst(/(\d+)/, -> $/{ sprintf( "%s%c%s",0 , $0.chars, $0)}, :g).lc; } return %cache{$word}; } sub natural_cmp ($a, $b) { return (pre_process($a) cmp pre_process($b)); }