The body is 93 characters. Can handle anything Perl's RE engine can call a character (including null and return), but like
chipmunk horribly non-strict and assumes you will not see characters that are not listed in the alphabet.
sub o{
($c,$s)=@_;@m{@$c}=map{10*@$c+$_}0..@$c;sub t{$_=pop;s/./$m{$&}/gs;$_}
+sort{t($a)cmp t($b)}@$s
}
Two subtle details. The /s is needed if returns are in the alphabet. And you cannot save a character by replacing string with numerical comparison without breaking on fairly small words.
UPDATE
MeowChow is right. :-( But the following 91 character
version will handle alphabets of up to 90,000 characters.
Given that this is bigger than Unicode's space, that is sufficient for any alphabet that any version of Perl will accept:
sub o{
($c,$s)=@_;@m{@$c}=map{$_+9999}1..@$c;sub t{$_=pop;s/./$m{$&}/gs;$_}so
+rt{t($a)cmp t($b)}@$s
}
(And so we create more legacy code with a stupid hard-coded limit that is bound to come back to bite us...)
UPDATE 2
MeowChow told me to lose parens gaining 2, and to make the range several hundred million by rewriting the number. Also I commified a map. This falls to 88. Or change to "99" to make it 86 if you are willing to accept one-byte alphabets.
sub o{
($c,$s)=@_;@m{@$c}=map$_+9**9,1..@$c;sub t{$_=pop;s/./$m{$&}/gs;$_}sor
+t{t($a)cmp t$b}@$s
}
UPDATE 3
ChemBoy dropped to 87 by rewriting the number again:
sub o{
($c,$s)=@_;@m{@$c}=map$_+1e9,1..@$c;sub t{$_=pop;s/./$m{$&}/gs;$_}sort
+{t($a)cmp t$b}@$s
}
UPDATE 4
tye made it 86 with:
sub o{
($c,$s)=@_;@m{@$c}=map$_+~0,1..@$c;sub t{$_=pop;s/./$m{$&}/gs;$_}sort{
+t($a)cmp t$b}@$s
}
UPDATE 5
And I finish the round of golf by making it 83 with:
sub o{
($c,$s)=@_;@m{@$c}=1e8..@$c+1e8;sub t{$_=pop;s/./$m{$&}/gs;$_}sort{t($
+a)cmp t$b}@$s
}
(The ~0 trick doesn't work with ranges.)