Another way, using decorate-undecorate, and including the latest requirement for case-insensitive sorting (but see also Sort::Maker):

>perl -wMstrict -le "my $ar = [ qw(X1 Mt10 mT2 y20 Y1 Y10 mt20 x20 X10) ]; ;; my @sorted = map { undecorate($_) } sort map { print qq{'$_'}; $_ } map { decorate($_) } @$ar ; ;; print qq{'$_'} for @sorted; ;; use constant WIDTH => 10; use constant SEP => ':'; ;; sub decorate { my ($num) = $_[0] =~ m{ (\d+) }xms; my ($alpha) = $_[0] =~ m{ ([[:alpha:]]+) }xms; return sprintf '%0*d%s%s%s', WIDTH, $num, lc($alpha), SEP, $_[0]; } ;; sub undecorate { return substr $_[0], 1 + index $_[0], SEP; } " '0000000001x:X1' '0000000010mt:Mt10' '0000000002mt:mT2' '0000000020y:y20' '0000000001y:Y1' '0000000010y:Y10' '0000000020mt:mt20' '0000000020x:x20' '0000000010x:X10' 'X1' 'Y1' 'mT2' 'Mt10' 'X10' 'Y10' 'mt20' 'x20' 'y20'

Note: The  map { print qq{'$_'};  $_ } step above is just to show the effect of decoration.

Also: The two regexes could be combined into one in the  decorate() function.

Update: See also Re: Help Manipulating Sort with Subroutines for another variation of this 'pattern'.


In reply to Re: sorting by numbers then alphabetically by AnomalousMonk
in thread sorting by numbers then alphabetically by Anonymous Monk

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.