Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Sort problems

by ccn (Vicar)
on Dec 11, 2008 at 14:47 UTC ( [id://729699]=note: print w/replies, xml ) Need Help??


in reply to Sort problems

Schwartzian transform
my @new_list = map { $_->[0] } sort { $a->[1] cmp $b->[1] } map { /(a|b)(\d+)_(\d+)\[(\d+)\]/ or die "Can't par +se $_"; [$_, sprintf "$1%05d%05d%05d", $2, $3, $4]; } @split_list;

Make a sortable pair for each element of the array. Concatenate first letter with all the numbers in an element. Expand every number to 5 digits with leading zeros. Then sort the pairs alphabetically by second element and take original elements from sorted pairs

Replies are listed 'Best First'.
Re^2: Sort problems
by ikegami (Patriarch) on Dec 11, 2008 at 16:01 UTC

    More robust:

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

Re^2: Sort problems
by erez_ez (Acolyte) on Dec 11, 2008 at 15:51 UTC
    first of all, Thanks! It looks like he cant parse certain words. One example: buff1_12p5u[0]

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://729699]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (1)
As of 2024-04-19 00:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found