in reply to natural sort on array of arrays

I also tried to adapt something I found on this forum, but I don't fully understand it.

my @sorted_matrix = grep {s/(^|\D)0+(\d)/$1$2/g,1} sort {$a->[0] cmp $b->[0]} grep {s/(\d+)/sprintf"%06.6d",$1/ge,1} @matrix;

It zero-pads the numbers for the purposes of sorting and then removes the padding once the sort is complete. Sounds like a sensible approach.

Replies are listed 'Best First'.
Re^2: natural sort on array of arrays
by AnomalousMonk (Archbishop) on May 10, 2016 at 19:18 UTC
    my @sorted_matrix = grep {s/(^|\D)0+(\d)/$1$2/g,1} sort {$a->[0] cmp $b->[0]} grep {s/
    (\d+)/sprintf"%06.6d",$1/ge,1} @matrix;

    As it stands, the first  s///ge (with the sprintf) operates on (and destroys) the top-level array reference. It can fairly easily be made to work, but I wouldn't: it makes my skin crawl.


    Give a man a fish:  <%-{-{-{-<