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) //取得指定的排序属性
// 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
}
相关文章
C# OleDbDataReader快速数据读取方式(3种)
这篇文章主要介绍了C# OleDbDataReader快速数据读取方式(3种),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2019-12-12
C#中图片旋转和翻转(RotateFlipType)用法分析
这篇文章主要介绍了C#中图片旋转和翻转(RotateFlipType)用法,实例分析了C#图片旋转及翻转Image.RotateFlip方法属性的常用设置技巧,需要的朋友可以参考下2015-06-06
C#利用iTextSharp组件给PDF文档添加图片/文字水印
这篇文章主要给大家介绍了关于如何C#利用iTextSharp组件给PDF文档添加图片/文字水印的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2020-10-10
C#实现微信公众号群发消息(解决一天只能发一次的限制)实例分享
经过几天研究网上的代码和谢灿大神的帮忙,今天终于用C#实现了微信公众号群发消息,现在分享一下2013-09-09


最新评论