Possible Duplicate:
Why does string.Compare seem to handle accented characters inconsistently?
I have the following code
var s1 = "ABzzzzz2";
var s2 = "äbzzzzz1";
var cmp = StringComparison.InvariantCultureIgnoreCase;
Console.WriteLine(string.Compare(s1, 0, s2, 0, 7, cmp)); //prints -1
Console.WriteLine(string.Compare(s1, 0, s2, 0, 8, cmp)); //prints 1
How could it be that part of the first string is less than part of the second, while the whole first string is greater than the whole second one?
I've tested it on x64, .net 2.0, 3.5, 4.0
My theory is that the algorithm first normalizes strings and then does the comparison. According to this "äbzzzzz1" normalized as "abzzzzz1". First comparison in normalized form results equality but returning 0 would be inccorent since the actual strings are not equal. So it reverts to ordinal comparison and results -1.
In the second case, after normalization, it is clear that "abzzzzz2" is greater than "abzzzzz1" so the result is 1.
This approach also explains cases mentioned in this question For normalization details check MSDN page
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With