C#快速排序集合
来源:互联网 发布:工控网络拓扑图 编辑:程序博客网 时间:2024/05/18 16:19
C#实现集合排序类。
说明: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) //取得指定的排序属性 { 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 }
- C#快速排序集合
- 排序算法集合(3)- 快速排序原理及C#实现
- C# 集合排序
- C#集合排序
- C#算法 快速排序
- C# 快速排序
- 快速排序算法c#
- 快速排序---c#实现
- 快速排序c#
- C#快速排序
- C#快速排序算法
- C#实现快速排序
- C#快速排序
- C#快速排序
- C# 实现快速排序
- C#快速排序算法
- C# 快速排序
- 【C#数据结构】快速排序
- Exadata中最有用的功能-存储索引
- POJ 1579 Function Run Fun【记忆化搜索】
- Android蓝牙操作
- oracle 中group by和 order by的区别
- JavaScript【图片轮播】
- C#快速排序集合
- Android布局文件中常用的长度单位解析
- 为什么程序员不擅长估算时间?
- Titanium开源代码
- fuse的一些理解-备忘
- ZOJ 3157 逆序对
- Using netconsole to get remote trace message
- sicily--1438 Shopaholic
- 为什么仍有诸多CIO在云计算面前无动于衷