Why does string.Compare seem to handle accented characters inconsistently?

后端 未结 3 1967
孤城傲影
孤城傲影 2020-12-10 02:52

If I execute the following statement:

string.Compare(\"mun\", \"mün\", true, CultureInfo.InvariantCulture)

The result is \'-1\', indicating

3条回答
  •  醉梦人生
    2020-12-10 03:42

    As I understand this it is still somewhat consistent. When comparing using CultureInfo.InvariantCulture the umlaut character ü is treated like the non-accented character u.

    As the strings in your first example obviously are not equal the result will not be 0 but -1 (which seems to be a default value). In the second example Muntelier goes last because t follows c in the alphabet.

    I couldn't find any clear documentation in MSDN explaining these rules, but I found that

    string.Compare("mun", "mün", CultureInfo.InvariantCulture,  
        CompareOptions.StringSort);
    

    and

    string.Compare("Muntelier, Schweiz", "München, Deutschland", 
        CultureInfo.InvariantCulture, CompareOptions.StringSort);
    

    gives the desired result.

    Anyway, I think you'd be better off to base your sorting on a specific culture such as the current user's culture (if possible).

提交回复
热议问题