Bug in the string comparing of the .NET Framework

前端 未结 2 1681
陌清茗
陌清茗 2020-12-23 21:17

It\'s a requirement for any comparison sort to work that the underlying order operator is transitive and antisymmetric.

In .NET, that\'s not true for some strings:

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-23 22:14

    If correct sorting is so important in your problem, just use ordinal string comparison instead of culture-sensitive. Only this one guarantees transitive and antisymmetric comparing you want.

    What MSDN says:

    Specifying the StringComparison.Ordinal or StringComparison.OrdinalIgnoreCase value in a method call signifies a non-linguistic comparison in which the features of natural languages are ignored. Methods that are invoked with these StringComparison values base string operation decisions on simple byte comparisons instead of casing or equivalence tables that are parameterized by culture. In most cases, this approach best fits the intended interpretation of strings while making code faster and more reliable.

    And it works as expected:

        Console.WriteLine(String.Compare(x, y, StringComparison.Ordinal));  // -12309
        Console.WriteLine(String.Compare(y, x, StringComparison.Ordinal));  // 12309
    

    Yes, it doesn't explain why culture-sensitive comparison gives inconsistent results. Well, strange culture — strange result.

提交回复
热议问题