The c collection quicksort class implements code sharing

  • 2020-05-30 20:56:57
  • OfStack

Description:

1. Parameterization of collection type;

2. You can sort the objects in the collection according to their attributes, and pass in the attribute name;

Note: the property must implement the IComparable interface. int, datetime, string and other basic types in C# have already implemented the IComparable interface.


/// <summary>
    ///  Sort the collection, such as 
    /// List<User> users=new List<User>(){.......}
    /// ListSorter.SortList<list<User>,User>(ref users,"Name",SortDirection.Ascending);
    /// </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 (propertyinfo.Name == property)          // Gets the specified sort property 
             // http://www.cnblogs.com/sosoft/
                {
                    QuickSort<TCollection, TItem>(ref list, 0, list.Count - 1, propertyinfo, direction);
                }
            }
        }
        /// <summary>
        ///  Quicksort algorithm 
        /// </summary>
        /// <typeparam name="TCollection"> Collection type, need to be implemented Ilist<T> A collection of </typeparam>
        /// <typeparam name="TItem"> The type of the object in the collection </typeparam>
        /// <param name="list"> A collection of objects </param>
        /// <param name="left"> Starting position from 0 start </param>
        /// <param name="right"> Termination of the position </param>
        /// <param name="propertyinfo"> Properties of objects in the collection, properties must be implemented IComparable interface </param>
        /// <param name="direction"> Sort 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 == SortDirection.Ascending)
                    {
                        while (i < j && ((IComparable)propertyinfo.GetValue(key, null)).CompareTo((IComparable)propertyinfo.GetValue(list[j], null)) < 0)
                        {
                            j--;
                        }
                        if (i < j)
                        {
                            list[i] = list[j];
                            i++;
                        }
                        while (i < j && ((IComparable)propertyinfo.GetValue(key, null)).CompareTo((IComparable)propertyinfo.GetValue(list[i], null)) > 0)
                        {
                            i++;
                        }
                        if (i < j)
                        {
                            list[j] = list[i];
                            j--;
                        }
                        list[i] = key;
                    }
                    else
                    {
                        while (i < j && ((IComparable)propertyinfo.GetValue(key, null)).CompareTo((IComparable)propertyinfo.GetValue(list[j], null)) > 0)
                        {
                            j--;
                        }
                        if (i < j)
                        {
                            list[i] = list[j];
                            i++;
                        }
                        while (i < j && ((IComparable)propertyinfo.GetValue(key, null)).CompareTo((IComparable)propertyinfo.GetValue(list[i], null)) < 0)
                        {
                            i++;
                        }
                        if (i < j)
                        {
                            list[j] = list[i];
                            j--;
                        }
                        list[i] = key;
                    }
                }
                // Perform a recursive call  
                QuickSort<TCollection, TItem>(ref list, left, i - 1, propertyinfo, direction);
                QuickSort<TCollection, TItem>(ref list, i + 1, right, propertyinfo, direction);
            }
        }
    }
    /// <summary>
    ///  Sorting type 
    /// </summary>
    public enum SortDirection
    {
        Ascending,
        Descending
    }


Related articles: