FWIW, here's a "roll your own" approach to the "natural" sort problem (note some extra data groups):

c:\@Work\Perl\monks>perl -wMstrict -le "use Data::Dump qw(dd); ;; my @matrix = ( [ qw(A1a1 img1 x123 y123) ], [ qw(A21a1 img8 x888 y888) ], [ qw(A1a2 img2 x123 y456) ], [ qw(A2a1 img9 x999 y999) ], [ qw(A1a12 img3 x456 y789) ], [ qw(A10a1 img4 x456 y123) ], [ qw(A12a1 img5 x456 y456) ], ); dd \@matrix; ;; sub natural_field { my ($field) = @_; ;; my @fields = $field =~ m{ \A ([[:upper:]]) (\d+) ([[:lower:]]) (\d+) \z }xms or die qq{bad sort field: '$field'} ; return pack 'a N a N', @fields; } ;; my @sorted = map $_->[0], sort { $a->[1] cmp $b->[1] } map [ $_, natural_field($_->[0]) ], @matrix ; dd \@sorted; " [ ["A1a1", "img1", "x123", "y123"], ["A21a1", "img8", "x888", "y888"], ["A1a2", "img2", "x123", "y456"], ["A2a1", "img9", "x999", "y999"], ["A1a12", "img3", "x456", "y789"], ["A10a1", "img4", "x456", "y123"], ["A12a1", "img5", "x456", "y456"], ] [ ["A1a1", "img1", "x123", "y123"], ["A1a2", "img2", "x123", "y456"], ["A1a12", "img3", "x456", "y789"], ["A2a1", "img9", "x999", "y999"], ["A10a1", "img4", "x456", "y123"], ["A12a1", "img5", "x456", "y456"], ["A21a1", "img8", "x888", "y888"], ]

Update: As an afterthought, a GRT version that might be advantageous for really large arrays, and a testing framework:

c:\@Work\Perl\monks>perl -le "use warnings; use strict; ;; use Test::More 'no_plan'; use Test::NoWarnings; ;; use Data::Dump qw(dd); use List::Util qw(shuffle); ;; use constant GRT_DECORATION => 'a N a N'; use constant { DECORATOR => qq{ ${ \GRT_DECORATION } N}, UNDECORATOR => qq{x[ ${ \GRT_DECORATION } ] N}, }; ;; my @matrix = ( [ qw(A1a1 img1 x123 y123) ], [ qw(A21a1 img8 x888 y888) ], [ qw(A1a2 img2 x123 y456) ], [ qw(A2a1 img9 x999 y999) ], [ qw(B2a1 img7 x777 y777) ], [ qw(A1a12 img3 x456 y789) ], [ qw(A10a1 img4 x456 y123) ], [ qw(A12a1 img5 x456 y456) ], ); ;; sub decorate { my ($i, $ar) = @_; ;; my $sort_field = $ar->[$i][0]; my @sort_subfields = $sort_field =~ m{ \A ([[:upper:]]) (\d+) ([[:lower:]]) (\d+) \z }xms or die qq{bad sorting field: '$sort_field'} ; return pack DECORATOR, @sort_subfields, $i; } ;; sub undecorate { my ($decorated, $ar) = @_; ;; return $ar->[ unpack UNDECORATOR, $decorated ]; } ;; my @sorted = map undecorate($_, \@matrix), sort map decorate($_, \@matrix), shuffle 0 .. $#matrix ; ;; my $expected = [ ['A1a1', 'img1', 'x123', 'y123'], ['A1a2', 'img2', 'x123', 'y456'], ['A1a12', 'img3', 'x456', 'y789'], ['A2a1', 'img9', 'x999', 'y999'], ['A10a1', 'img4', 'x456', 'y123'], ['A12a1', 'img5', 'x456', 'y456'], ['A21a1', 'img8', 'x888', 'y888'], ['B2a1', 'img7', 'x777', 'y777'], ]; is_deeply \@sorted, $expected, 'sorted ok', ;; done_testing; " ok 1 - sorted ok 1..1 ok 2 - no warnings 1..2
More test cases, especially corner cases, wouldn't hurt.


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


In reply to Re: natural sort on array of arrays by AnomalousMonk
in thread natural sort on array of arrays by shamat

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.