@in = ( ['the cat', 'sat', 'on', 'the', 'mat'], ['the cat sat', 'wherever if felt like'], ['the cat'], ); my @out = map{$_->[0]} sort{$a->[1] cmp $b->[1]} map{[$_, concat($_)]} @in; sub concat { my $ary = shift; $ary = join '', @$ary; $ary =~ s/\s//g; return $ary; } use Data::Dumper; $Data::Dumper::Indent = 0; print map {s/\[/\n \[/g; $_} Dumper \@out; __DATA__ $VAR1 = [ ['the cat'], ['the cat','sat','on','the','mat'], ['the cat sat','wherever if felt like']];