filippo said:
I have string lilke these
a12-1
b1-3
b123-2
c12
c12-1
axxx-y
a is a letter
xxx is a number 1-120
y is a number 1-3
I want to sort by xxx, is it possible?
Sure it is. In your sort subroutine, compare the xxx parts of the
string, rather than the strings themselves:
my @sorted = sort {
my ($a_num) = $a =~ /^[a-z](\d+)/;
my ($b_num) = $b =~ /^[a-z](\d+)/;
return $a_num <=> $b_num;
} @strings;
Or, to make this more efficient, use a Schwartzian Transform, so you
only have to do the pattern match once on each element:
my @sorted = map { $_->[0] }
sort { $a->[1] <=> $b->[1] }
map { [$_, /^[a-z](\d+)/ ] } @strings;
(Note that both of these will leave a12-1, c12, and c12-1 in a random
order in Perl 5.6 and lower, and in the same order as they were in the
original list in Perl 5.8 and higher. Altering the sort to take the
second number and/or the beginning letter into account is left as an
excercise to the OP.
)
Paul Lalli