my $sortstr = join('', @list); my @sorted_list = sort { rindex($sortstr,substr($b,0,1)) <=> rindex($sortstr,substr($a,0,1)) || $a cmp $b } @list2;