Normally if a character A is smaller than character B, then I'd expect in string comparison:
AB < BA
And indeed, in a dictionary you can find AB words before BA words.
Somehow this is not the case for underscores
Small program; use copy paste to check
public static void Main()
{
IComparer<string> comp = Comparer<string>.Default;
Console.WriteLine("Compare '-' with '_': " + comp.Compare("-", "_").ToString());
Console.WriteLine("Compare '--' with '-_': " + comp.Compare("--", "-_").ToString());
Console.WriteLine("Compare '-_' with '_-': " + comp.Compare("-_", "_-").ToString());
}
The output is:
Compare '-' with '_': -1
Compare '--' with '-_': -1
Compare '-_' with '_-': 1
The last value returns +1
So:
"-"< "_" // minus is less than underscore
"-_" > "_-" // minus underscore MORE THAN underscore minus
This behaviour occurs when using the default string comparer. StringComparer.Ordinal works as expected.
Is this an error in the default string comparer?