in reply to Re^2: sorting an array mix
in thread sorting an array mix

You can dodge the zeros issue and make it a bit more efficient using pack
Yes, up to a point. It'll fail if one of the numbers exceed 4294967295 (2^32, the maximum number that will fit in an unsigned long).

It also assumes that all the strings start with the same letters, or that if they don't, the non-digit prefix is the secondary key; my solution assumes it's the primary key.

That is, given the list

@g = qw [foo11 baz39 bar23 baz7 bar13 foo23];
your solution sorts it as:
baz7 foo11 bar13 bar23 foo23 baz39
and mine as
bar13 bar23 baz7 baz39 foo11 foo23
From the example the OP gives, it's not clear what he wants (or even whether the difference is relevant).

Replies are listed 'Best First'.
Re^4: sorting an array mix
by BrowserUk (Patriarch) on Oct 08, 2008 at 23:16 UTC
    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.