Search code examples
c#sortingstring-comparison

No difference between CurrentCulture and CurrentCultureIgnoreCase, why?


I am trying to sort a sequence of letters using the Order LINQ operator, and I am not getting the expected result. I was expecting that passing the StringComparer.CurrentCulture would produce a different order than passing the StringComparer.CurrentCultureIgnoreCase, but both seem to ignore the case of my letters:

string[] letters = ["y", "X", "Z"];
Console.WriteLine(String.Join(", ", letters.Order(StringComparer.CurrentCulture)));
Console.WriteLine(String.Join(", ", letters.Order(StringComparer.CurrentCultureIgnoreCase)));

Output:

X, y, Z
X, y, Z

Online demo.

I was expecting that the CurrentCulture would be sensitive to the case of the letters, and would produce this order: X, Z, y, because the small English letters are farther in the ASCII chart than the capital English letters. What am I not understanding correctly?


Solution

  • The difference of StringComparer.CurrentCulture and StringComparer.CurrentCultureIgnoreCase is relevant when you have two values which have the "same" string/text, but may have different cases. In your example ["y", "X", "Z"] the strings are all "different", as "x" still comes before "y" and "y" still comes before "z". And this has nothing to do with the cases of these strings.

    But when the sorting algorithm sees a list like ["x", "X", "X", "x", "x", "X"] there is a difference if the values "x" and "X" should be the same regarding sorting (so the Compare() method returns 0) or if they should be different (so the Compare() method will return -1/1 or any other value smaller/bigger zero).

    When you use the following code:

    string[] letters = ["y", "X", "Z", "X", "x", "y", "Y", "z", "Z", "y", "Y", "z", "Z"];
    Console.WriteLine(String.Join(", ", letters.Order(StringComparer.CurrentCulture)));
    Console.WriteLine(String.Join(", ", letters.Order(StringComparer.CurrentCultureIgnoreCase)));
    

    you will get the following result:

    x, X, X, y, y, y, Y, Y, z, z, Z, Z, Z
    X, X, x, y, y, Y, y, Y, Z, z, Z, z, Z
    

    In the second line, all the "same" letters are equal to each other, the case is literally ignored as instructed. And since the sorting algorithm is stable, the relative order of the "same" letters are still in the same original order (here on the letter "y"):

    ["y", "X", "Z", "X", "x", "y", "Y", "z", "Z", "y", "Y", "z", "Z"]
      ^                        ^    ^              ^    ^
      1                        2    3              4    5
    
    X, X, x, y, y, Y, y, Y, Z, z, Z, z, Z
             ^  ^  ^  ^  ^
             1  2  3  4  5