Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: how to sort this?

by ikegami (Pope)
on Mar 25, 2006 at 05:41 UTC ( #539145=note: print w/replies, xml ) Need Help??


in reply to how to sort this?

The following sorts numerically by the number preceeding the 'x', then numerically by the number following the 'x':

@a = sort { my ($a1, $a2) = split(/x/, $a); my ($b1, $b2) = split(/x/, $b); $a1 <=> $b1 || $a2 <=> $b2 } @a;

Replies are listed 'Best First'.
Re^2: how to sort this?
by bobf (Monsignor) on Mar 25, 2006 at 06:00 UTC

    The OP said the elements contain "both digits & characters", which I assume means (one or more digits) followed by (one or more alpha chars) followed by (one or more digits). If that's the case, ikegami's sort routine can be generalized to include the alpha component:

    @a = sort { my ($a1, $a2, $a3) = ( $a =~ m/(\d+)(\D+)(\d+)/ ); my ($b1, $b2, $b3) = ( $b =~ m/(\d+)(\D+)(\d+)/ ); $a1 <=> $b1 || $a2 cmp $b2 || $a3 <=> $b3 } @a;

    The OP wasn't clear about the specifics, though. If there really is just a single 'x' between the digit portions (and it's always an 'x'), the generalization isn't needed. If by "characters" the OP meant "any character" (as opposed to only alpha chars), then things get more complex.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://539145]
help
Chatterbox?
[Discipulus]: upvote me! i'll make pm great again! ;=)
[shmem]: Discipulus reminds me of an irish saying: "I would guess a tomorrow can wait 'til this day is done" :-)
[LanX]: nite! :)
[Discipulus]: or a spanish one: ' qui mucho se despide no quiere despedirse'
shmem slides into the envelope
[shmem]: Discipulus *quien*
[Discipulus]: quien ..
[Discipulus]: me too, i need a Dumas time, see you

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2017-04-29 22:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I'm a fool:











    Results (534 votes). Check out past polls.