How Do I Sort IList?

后端 未结 6 1264
终归单人心
终归单人心 2021-01-11 23:14

There\'s no Sort() function for IList. Can someoene help me with this? I want to sort my own IList.

Suppose this is my IList:



        
6条回答
  •  被撕碎了的回忆
    2021-01-11 23:54

    Use OrderBy

    Example

    public class MyObject() 
    {
        public int number { get; set; }
        public string marker { get; set; }
    }
    
    IList myobj = new List();
    var orderedList = myobj.OrderBy(x => x.marker).ToList();
    

    For a case insensitive you should use a IComparer

    public class CaseInsensitiveComparer : IComparer
    {
        public int Compare(string x, string y)
        {
            return string.Compare(x, y, StringComparison.OrdinalIgnoreCase);
        }
    }
    
    IList myobj = new List();
    var orderedList = myobj.OrderBy(x => x.marker, new CaseInsensitiveComparer()).ToList();
    

提交回复
热议问题