How Do I Sort IList?

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

    To sort in-place you would essentially see these two approaches:

            IList list = .... // your ilist
            var sorted = list.ToArray();
            Array.Sort(sorted);
    
            for (int i = 0; i < list.Count; i++)
            {
                list[i] = sorted[i];
            }
    

    and

            IList list = .... // your ilist
            ArrayList.Adapter((IList)list).Sort();
    

    The second one might look simpler but won't be great for value type collections since it incur boxing penalties. Furthermore there is no guarantee your IList will be implementing IList. First one is better IMO.


    You can also use the first approach to sort an ICollection in-place but it is questionable if you should expose such a functionality since ICollection contract doesn't guarantee an order (think hash structures). Anyway to show you code example:

        ICollection collection = .... // your icollection
        var sorted = collection.ToArray();
        Array.Sort(sorted);
    
        collection.Clear();
        foreach (var i in sorted)
        {
           collection.Add(i);
        }
    

    A note on sort stability, .NET's Array/List sorting algorithms are unstable. For a stable sort you will have to use:

            IList list = .... // your ilist
            var sorted = list.OrderBy(i => i).ToArray();
    
            for (int i = 0; i < list.Count; i++)
            {
                list[i] = sorted[i];
            }
    

    This can't be as fast as unstable sorts.


    Finally, for a complete answer, perhaps a composite approach taken by watbywbarif is better:

        public static void Sort(this IList list, IComparer comparer, bool stable)
        {
            if (stable)
            {
                list.StableSort(comparer);
            }
            else
            {
                list.UnstableSort(comparer);
            }
        }
    
        static void StableSort(this IList list, IComparer comparer)
        {
            list.OrderBy(x => x, comparer).CopyTo(list);
        }
    
        static void UnstableSort(this IList list, IComparer comparer)
        {
            switch (list)
            {
                case List l:
                    l.Sort(comparer);
                    break;
    
                case T[] a:
                    Array.Sort(a, comparer);
                    break;
    
                default:
                    T[] sortable = list.ToArray();
                    sortable.UnstableSort(comparer);
                    sortable.CopyTo(list);
                    break;
            }
        }
    
        static void CopyTo(this IEnumerable source, IList target)
        {
            int i = 0;
            foreach (T item in source)
            {
                target[i++] = item;
            }
        }
    

    That's as far as built-in approaches go. For faster implemenation you will have to roll out your own, see: https://stackoverflow.com/a/19167475

提交回复
热议问题