How Do I Sort IList?

后端 未结 6 1267
终归单人心
终归单人心 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:56

    For explanation why not to use OrderBy or similar check Christophe's answer.

    Here is one attempt to make fast Sort:

    public static void Sort(this IList ilist)
    {
        switch(ilist)
        {
            case List lst:
                lst.Sort();
                break;
            case Array arr:
                Array.Sort(arr);
                break;
            default:
                throw new NotImplementedException();
                // or add slow impl if you don't want this to fail!!
        }
    }
    

提交回复
热议问题