It'll fail if one of the numbers exceed 4294967295 (2^32, the maximum number that will fit in an unsigned long).

Mine, like yours, is tailored to the OPs data. However, yours has a flaw that will manifest itself even within the confines of the OPs data format.

It doesn't preserve pre-exist leading zeros:

print for map{ s/\D\K0+//; $_ } sort map{ sprintf "%s%02d", /(\D+)(\d+)/ } qw[ St0 St01 St00009 ];; St St1 St9

For a generic solution, you can take the pack solution to the next level, deal with 2^32 issue and allow for multiple numeric fields:

my @g = qw[ St1x7 St1x23 St10x355 Stb10x100 Sta2x9 St2x1 Stbbb12x5 Staaa12x4 S +t6x33333 St6x0 St8 St0x11111111111111 St0x22222222222222 AAAAAAAAAAAAAAAAAA + ZZZZZZZZZZZ ];; print for map{ my $n = unpack 'N', substr $_, -4; substr $_, $n, length() - $n - 4 } sort map{ (my $x = $_) =~ s[(\d{,9})][ pack 'N', $1]ge; pack 'A*A*N', $x, $_, length( $x ) } @g;; AAAAAAAAAAAAAAAAAA St0x11111111111111 St0x22222222222222 St10x355 St1x23 St1x7 St2x1 St6x0 St6x33333 St8 Sta2x9 Staaa12x4 Stb10x100 Stbbb12x5 ZZZZZZZZZZZ

But the extra code involved will slow things down, so you only use it if you need to, or when you do not know in advanced the format of the data.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

In reply to Re^4: sorting an array mix by BrowserUk
in thread sorting an array mix by baxy77bax

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.