LINQ and CASE Sensitivity

后端 未结 4 963
花落未央
花落未央 2021-01-22 16:57

I have this LINQ Query:

TempRecordList = new ArrayList(TempRecordList.Cast().OrderBy(s => s.Substring(9, 30)).ToArray());

It w

相关标签:
4条回答
  • 2021-01-22 17:53

    As already mentioned, the OrderBy() method takes a comparer as a second parameter.

    For strings, you don't necessarily have to implement an IComparer<string>. You might be fine with System.StringComparer.CurrentCulture (or one of the others in System.StringComparer).

    In your exact case, however, there is no built-in comparer which will handle also the - after letter sort order.

    0 讨论(0)
  • 2021-01-22 17:58

    To customize the sorting order you will need to create a comparer class that implements IComparer<string> interface. The OrderBy() method takes comparer as second parameter.

    internal sealed class NameComparer : IComparer<string> {
        private static readonly NameComparer DefaultInstance = new NameComparer();
    
        static NameComparer() { }
        private NameComparer() { }
    
        public static NameComparer Default {
            get { return DefaultInstance; }
        }
    
        public int Compare(string x, string y) {
            int length = Math.Min(x.Length, y.Length);
            for (int i = 0; i < length; ++i) {
                if (x[i] == y[i]) continue;
                if (x[i] == '-') return 1;
                if (y[i] == '-') return -1;
                return x[i].CompareTo(y[i]);
            }
    
            return x.Length - y.Length;
        }
    }
    

    This works at least with the following test cases:

    var names = new[] {
        "Palmer-Johnson, Sean",
        "Palm-Bouter, Peter",
        "Dias, Reginald",
        "DiBlackley, Anton",
    };
    
    var sorted = names.OrderBy(name => name, NameComparer.Default).ToList();
    
    // sorted:
    // [0]: "DiBlackley, Anton"
    // [1]: "Dias, Reginald"
    // [2]: "Palmer-Johnson, Sean"
    // [3]: "Palm-Bouter, Peter"
    
    0 讨论(0)
  • 2021-01-22 17:59

    You might want to just implement a custom IComparer object that will give a custom priority to special, upper-case and lower-case characters.

    http://msdn.microsoft.com/en-us/library/system.collections.icomparer.aspx

    0 讨论(0)
  • 2021-01-22 18:01

    OrderBy() returns results in ascending order.

    e comes before h, thus the first result (remember you're comparing on a substring that starts with the character in the 9th position...not the beginning of the string) and i comes before y, thus the second. Case sensitivity has nothing to do with it.

    If you want results in descending order, you should use OrderByDescending():

    TempRecordList.Cast<string>
                  .OrderByDescending(s => s.Substring(9, 30)).ToArray());
    
    0 讨论(0)
提交回复
热议问题