SoFunction
Updated on 2025-03-06

C# collection quick sort class implementation code sharing


/// <summary>
/// Sort the collection, such as
    /// List<User> users=new List<User>(){.......}
    /// <list<User>,User>(ref users,"Name",);
    /// </summary>
    public class ListSorter
    {
        public static void SortList<TCollection, TItem>(ref TCollection list, string property, SortDirection direction) where TCollection : IList<TItem>
        {
            PropertyInfo[] propertyinfos = typeof(TItem).GetProperties();
            foreach (PropertyInfo propertyinfo in propertyinfos)
            {
if ( == property) //Get the specified sorting attribute
             // /sosoft/
                {
                    QuickSort<TCollection, TItem>(ref list, 0, - 1, propertyinfo, direction);
                }
            }
        }
        /// <summary>
/// Quick sorting algorithm
        /// </summary>
/// <typeparam name="TCollection">collection type, Ilist<T>collection</typeparam> needs to be implemented
/// <typeparam name="TItem">Type of object in collection</typeparam>
/// <param name="list">collection object</param>
/// <param name="left">First position, starting from 0</param>
/// <param name="right">termination position</param>
/// <param name="propertyinfo">Properties of objects in the collection, properties must implement the IComparable interface</param>
/// <param name="direction">Sorting type (ascending or descending)</param>
        private static void QuickSort<TCollection, TItem>(ref TCollection list, int left, int right, PropertyInfo propertyinfo, SortDirection direction) where TCollection : IList<TItem>
        {
            if (left < right)
            {
                int i = left, j = right;
                TItem key = list[left];
                while (i < j)
                {
                    if (direction == )
                    {
                        while (i < j && ((IComparable)(key, null)).CompareTo((IComparable)(list[j], null)) < 0)
                        {
                            j--;
                        }
                        if (i < j)
                        {
                            list[i] = list[j];
                            i++;
                        }

                        while (i < j && ((IComparable)(key, null)).CompareTo((IComparable)(list[i], null)) > 0)
                        {
                            i++;
                        }
                        if (i < j)
                        {
                            list[j] = list[i];
                            j--;
                        }
                        list[i] = key;
                    }
                    else
                    {
                        while (i < j && ((IComparable)(key, null)).CompareTo((IComparable)(list[j], null)) > 0)
                        {
                            j--;
                        }
                        if (i < j)
                        {
                            list[i] = list[j];
                            i++;
                        }

                        while (i < j && ((IComparable)(key, null)).CompareTo((IComparable)(list[i], null)) < 0)
                        {
                            i++;
                        }
                        if (i < j)
                        {
                            list[j] = list[i];
                            j--;
                        }
                        list[i] = key;
                    }
                }
//Execute recursive calls
                QuickSort<TCollection, TItem>(ref list, left, i - 1, propertyinfo, direction);
                QuickSort<TCollection, TItem>(ref list, i + 1, right, propertyinfo, direction);
            }
        }
    }
    /// <summary>
/// Sort type
    /// </summary>
    public enum SortDirection
    {
        Ascending,
        Descending
    }