Brad> I think 11. A D should come before 10. B D, because all else being equal,
Brad> A comes before B. In addition, when your code expands the terms 'A'..'E',
Brad> you get:
Brad> A B C D E
Brad> A B C D
Brad> B C D E
Brad> A B C E
Brad> A B D E
Brad> A C D E
Brad> A B C
Brad> B C D
Brad> C D E
Brad> ...
Brad> While 'A B D E' has more terms, I think 'A B C', 'B C D', and 'C D E'
Brad> should outrank it, because they have more adjacent terms in a row.
It appears as though you are sorting on "edit distance", which is a
well-defined term, and even has a module, String::Approx, to compute
it.
use strict;
use String::Approx 'adist';
my @strings = glob "{,A}{,B}{,C}{,D}{,E}"; # lazy.
shift @strings; # leave out empty string
my @dists = map { abs adist("ABCDE", $_) } @strings;
my @sorted = sort {
$dists[$a] <=> $dists[$b] or $strings[$a] cmp $strings[$b]
} 0..$#strings;
printf "%5s %d\n", $strings[$_], $dists[$_] for @sorted;
==>
ABCDE 0
ABCD 1
ABCE 1
ABDE 1
ACDE 1
BCDE 1
ABC 2
ABD 2
ABE 2
ACD 2
ACE 2
ADE 2
BCD 2
BCE 2
BDE 2
CDE 2
AB 3
AC 3
AD 3
AE 3
BC 3
BD 3
BE 3
CD 3
CE 3
DE 3
A 4
B 4
C 4
D 4
E 4