欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c#集合快速排序类实现代码分享

程序员文章站 2024-02-19 22:03:16
说明: 1、集合类型参数化; 2、可根据集合中的对象的各个属性进行排序,传入属性名称即可; 注:属性必须实现了icomparable接口,c#中int、datetim...

说明:

1、集合类型参数化;

2、可根据集合中的对象的各个属性进行排序,传入属性名称即可;

注:属性必须实现了icomparable接口,c#中int、datetime、string等基本类型都已经实现了icomparable接口。

复制代码 代码如下:

/// <summary>
    /// 对集合进行排序,如
    /// 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)          //取得指定的排序属性
             // http://www.cnblogs.com/sosoft/
                {
                    quicksort<tcollection, titem>(ref list, 0, list.count - 1, propertyinfo, direction);
                }
            }
        }
        /// <summary>
        /// 快速排序算法
        /// </summary>
        /// <typeparam name="tcollection">集合类型,需要实现ilist<t>集合</typeparam>
        /// <typeparam name="titem">集合中对象的类型</typeparam>
        /// <param name="list">集合对象</param>
        /// <param name="left">起始位置,从0开始</param>
        /// <param name="right">终止位置</param>
        /// <param name="propertyinfo">集合中对象的属性,属性必须要实现icomparable接口</param>
        /// <param name="direction">排序类型(升序或降序)</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;
                    }
                }
                //执行递归调用
                quicksort<tcollection, titem>(ref list, left, i - 1, propertyinfo, direction);
                quicksort<tcollection, titem>(ref list, i + 1, right, propertyinfo, direction);
            }
        }
    }
    /// <summary>
    /// 排序类型
    /// </summary>
    public enum sortdirection
    {
        ascending,
        descending
    }