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

时间:2023-03-30 13:38:51 

说明:

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
    }

标签:集合快速排序
0
投稿

猜你喜欢

  • Java锁擦除与锁粗化概念和使用详解

    2022-02-09 15:32:30
  • 初识Spring Boot框架和快速入门

    2022-10-17 00:58:52
  • android打开本地图像的方法

    2022-10-26 08:01:42
  • 使用java采集京东商城行政区划数据示例

    2023-04-17 06:31:52
  • Java mybatis 开发自定义插件

    2022-11-26 03:29:24
  • C#实现AddRange为数组添加多个元素的方法

    2023-06-26 13:34:25
  • android通过自定义toast实现悬浮通知效果的示例代码

    2022-08-11 03:23:54
  • java使用hadoop实现关联商品统计

    2022-11-05 05:55:43
  • SpringBoot实现过滤器和拦截器的方法

    2022-10-21 23:29:34
  • 整理C# 二进制,十进制,十六进制 互转

    2023-10-01 13:44:08
  • Java实现企业发放的奖金根据利润提成问题

    2023-02-11 06:52:33
  • Java Gradle项目中的资源正确获取方式

    2022-10-05 09:00:50
  • Android互联网访问图片并在客户端显示的方法

    2021-12-26 21:25:10
  • 如何使用JaCoCo分析java单元测试覆盖率

    2023-01-13 11:38:39
  • Java获取时间年、月、日的方法

    2022-02-10 16:58:36
  • Java日期时间字符串和毫秒相互转换的方法

    2022-03-11 18:15:57
  • Android Retrofit原理深入探索

    2023-09-06 02:47:15
  • C#封装的Sqlite访问类实例

    2022-04-28 15:38:13
  • C#微信开发之启用开发者模式

    2022-07-07 11:24:54
  • Java实现多人聊天室(含界面)

    2021-07-15 23:18:36
  • asp之家 软件编程 m.aspxhome.com