While you can sort it automagically (as jdporter already says) with just sort { $a <=> $b } @list, you also can do it in two lines (but still they're simpler than those regexps). First of all, prepare your data so you can use simpler tools to process it. For each list member you have two fields of data in one string, and for me it's a hash that looked at me from that @list.
my @list = ( '1,cat', '2,dog', '22,mouse', '11,eel', '001,elk', '13,mi
+nk');
my %unsorted = map { split /,/ } @list ;
my @sorted = map { $_.','.$unsorted{$_} } sort { $a <=> $b } keys %uns
+orted;
But maybe we're not so lucky and our data will not be prepended with right numerical indexes. What to do? Just reverse the hash pair!
my @list = ( 'cat,1', 'dog,2', 'mouse,22', 'eel,11', 'elk,001', 'mink,
+13');
my %unsorted = map { reverse split /,/ } @list ;
my @sorted = map { $unsorted{$_}.','.$_ } sort { $a <=> $b } keys %uns
+orted;
Now, what if we're totally unlucky and our numerical index not only sit in the end of string, but sometimes equals to another index? We will lose some data in a hash, if we just split index from string and make pairs from them! Could the code still be two lines and still solve the problem and remain readable and understandable?
my @list = ( 'cat,1', 'dog,1', 'mouse,22', 'eel,22', 'elk,001', 'mink,
+13');
# as pointed by choroba, this can be put simpler
# my %unsorted = map { $_ => [ reverse split /,/ ]->[0] } @list ;
my %unsorted = map { $_ => [ split /,/ ]->[-1] } @list ;
my @sorted = sort { $unsorted{$a} <=> $unsorted{$b} } keys %unsorted;
TIMTOWTDI!
p.s.: Updated last code as
choroba pointed.