my @sorted = map { $_->[0] } sort { $a->[1] <=> $b->[1] } map{ /chr(.+)/; if ($1 eq "X") { $1=23 }; if ($1 eq "Y") { $1=24 }; $_=[$_,$1] } @unsorted;