my @sentences1 = map { /(\w+)\s+searching/; [$1, $_] } @sentences; my @sentences2 = sort { $a->[0] cmp $b->[0] } @sentences1; my @sentences3 = map {$_->[1]} @sentences2; foreach (@sentences3) { print $_; }