here is something small you might like to try if
halley's hash above proves too chunky for your ZX81.
the following will deposit the sorted subset
in @uS; the memory consumued will be of the
order of the size of elements in the unsorted
list. so it will be kind to machines with less RAM.
if the unsorted list is short, the fact that the only a few
grep's will be employed will make the solution
reasonable in time too.
my @S=("tilly","zaxo","sauog","enlil","castaway","wufnik");
my @U = ("castaway","zaxo","wufnik","enlil");
my %uS = map { $tmp = $_; $tmp => grep { $S[$_] =~ /^$tmp$/ } (0..$#S)
+ } @U;
@uS = sort { $uS{$a} <=> $uS{$b} } @U;
print join "\n", @uS;
which produces
zaxo
enlil
castaway
wufnik
from the unsorted list above.
it freely admit it won't always be the fastest solution, but it will be kind, at least to those who like map & grep.
...wufnik
-- in the world of the mules there are no rules --