http://www.perlmonks.org?node_id=539149


in reply to Re: how to sort this?
in thread how to sort this?

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.