How does OrderBy work with regard to strings in C#?

前端 未结 3 1112
-上瘾入骨i
-上瘾入骨i 2021-01-02 02:12

Consider this code

var strings2 = new List
    {
        \"0\", // Ascii code 48 (decimal)
        \"|\"  // Ascii code 125 (decimal)
    };
va         


        
相关标签:
3条回答
  • 2021-01-02 02:33

    Here you go:

    The comparison uses the current culture to obtain culture-specific information such as casing rules and the alphabetic order of individual characters. For example, a culture could specify that certain combinations of characters be treated as a single character, or uppercase and lowercase characters be compared in a particular way, or that the sorting order of a character depends on the characters that precede or follow it.

    Source: String.Compare Method on MSDN

    0 讨论(0)
  • 2021-01-02 02:39

    The ".OrderBy" function utilizes the default comparer for a string. That comparer is not necessarily going to return a sort order based on the ASCII code.

    For a list of all the different string comparers, see the article on MSDN.

    0 讨论(0)
  • The order depends on the culture that you use.

    You can pass the culture in an overload to OrderBy.

    var sorted = strings2.OrderBy(x => x, StringComparer.InvariantCulture)
    
    0 讨论(0)
提交回复
热议问题