Help for this page

Select Code to Download


  1. or download this
    sub natural_sort {
        return @_ if @_ < 2;
        my $index = 0;
        return @_[@_».lc».subst(/(\d+)/, -> $/{ sprintf( "%s%c%s", 0, $0.c
    +hars, $0) }, :g).map( *~' '~$index++ ).sort.map({@(split(' ',$^_))[*-
    +1]})];
    }
    
  2. or download this
    sub natural_cmp ($a, $b) {
       return [leg] ($a, $b)».lc».subst(/(\d+)/, -> $/{ sprintf( "%s%c%s",
    + 0, $0.chars, $0) }, :g);
    }
    
  3. or download this
    my %cache;
    sub cached_natural_cmp ($a, $b) {
        return [leg] ($a, $b)».lc.map( %cache{ * } //= *.subst(/(\d+)/, ->
    + $/{ sprintf( "%s%c%s", 0, $0.chars, $0) }, :g));
    }
    
  4. or download this
     @array.sort(*.lc.subst(/(\d+)/, -> $/{ sprintf( "%s%c%s", 0, $0.chars
    +, $0) }, :g));
    
  5. or download this
    @array.sort: natural(*);
       
    sub natural ($) { *.lc.subst(/(\d+)/, -> $/{ sprintf( "%s%c%s", 0, $0.
    +chars, $0) }, :g) }
    
  6. or download this
    .say for %words.keys.sort: natural(*);
    
    or 
    
    %words.keys.sort( natural(*) )».say;