c#集合快速排序類實現(xiàn)代碼分享
說明:
1、集合類型參數(shù)化;
2、可根據(jù)集合中的對象的各個屬性進行排序,傳入屬性名稱即可;
注:屬性必須實現(xiàn)了IComparable接口,C#中int、datetime、string等基本類型都已經(jīng)實現(xiàn)了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) //取得指定的排序?qū)傩?BR> // http://www.cnblogs.com/sosoft/
{
QuickSort<TCollection, TItem>(ref list, 0, list.Count - 1, propertyinfo, direction);
}
}
}
/// <summary>
/// 快速排序算法
/// </summary>
/// <typeparam name="TCollection">集合類型,需要實現(xiàn)Ilist<T>集合</typeparam>
/// <typeparam name="TItem">集合中對象的類型</typeparam>
/// <param name="list">集合對象</param>
/// <param name="left">起始位置,從0開始</param>
/// <param name="right">終止位置</param>
/// <param name="propertyinfo">集合中對象的屬性,屬性必須要實現(xiàn)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;
}
}
//執(zhí)行遞歸調(diào)用
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
}
相關(guān)文章
C#基于Linq和反射實現(xiàn)數(shù)據(jù)持久化框架Xml4DB詳解
在本篇文章里小編給大家整理的是關(guān)于C#基于Linq和反射實現(xiàn)數(shù)據(jù)持久化框架Xml4DB相關(guān)知識點,有需要的朋友們學習下。2019-08-08C#實現(xiàn)壓縮和解壓縮的方法示例【Gzip和Zip方式】
這篇文章主要介紹了C#實現(xiàn)壓縮和解壓縮的方法,結(jié)合具體實例形式分析了Gzip和Zip兩種壓縮操作實現(xiàn)方法,需要的朋友可以參考下2017-06-06C#實現(xiàn)的簡單隨機數(shù)產(chǎn)生器功能示例
這篇文章主要介紹了C#實現(xiàn)的簡單隨機數(shù)產(chǎn)生器功能,涉及C#簡單界面布局、事件響應及隨機數(shù)生成相關(guān)操作技巧,需要的朋友可以參考下2017-09-09c#使用微信接口開發(fā)微信門戶應用中微信消息的處理和應答
這篇文章主要介紹了c#使用微信接口開發(fā)微信門戶中的微信消息的處理和應答的過程,需要的朋友可以參考下2014-03-03