in reply to Re: Sort problems
in thread Sort problems

More robust:

my @new_list = map { $_->[0] } sort { $a->[1] cmp $b->[1] } map { ( my $s = $_ ) =~ s/(\d+)/0$1/g; [ $_, $s ] } @split_list;

Replies are listed 'Best First'.
Re^3: Sort problems
by erez_ez (Acolyte) on Dec 11, 2008 at 16:19 UTC
    Thanks, but still it didnt do it right. Take this list for example, the result:
    buff1_100u[1] buff1_12p5u[0] buff1_25u[6] buff1_50u[4] buff2_100u[3] b +uff2_12p5u[2] buff2_25u[3] buff2_50u[1]
    buff1_100u1 cant come before buff1_25u6...

      For some reason, I thought the leading zero would address that. What was I thinking? Moving on...

      map { ( my $s = $_ ) =~ s/(\d+)/ sprintf('%010d%s', length($1), $1) /eg; [ $_, $s ] } }

      One could use pack instead of sprintf, but it won't work with unicode semantics. That's already a problem, but it'll be even worse in 5.12 because I believe unicode semantics will be used for all strings instead of just those internally encoded as UTF-8 (by default).

        It worked great! Thanks! One more thing, what if I want to sort the numbers inside the brackets from top to bottom? I mean, the same sort you did for the word but opposite sort to the numbers in the brackets:
        a[2] a[1] b[1] b[0]