c#集合快速排序類實(shí)現(xiàn)代碼分享
說明:
1、集合類型參數(shù)化;
2、可根據(jù)集合中的對(duì)象的各個(gè)屬性進(jìn)行排序,傳入屬性名稱即可;
注:屬性必須實(shí)現(xiàn)了IComparable接口,C#中int、datetime、string等基本類型都已經(jīng)實(shí)現(xiàn)了IComparable接口。
/// <summary>
/// 對(duì)集合進(jìn)行排序,如
/// 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">集合類型,需要實(shí)現(xiàn)Ilist<T>集合</typeparam>
/// <typeparam name="TItem">集合中對(duì)象的類型</typeparam>
/// <param name="list">集合對(duì)象</param>
/// <param name="left">起始位置,從0開始</param>
/// <param name="right">終止位置</param>
/// <param name="propertyinfo">集合中對(duì)象的屬性,屬性必須要實(shí)現(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# OleDbDataReader快速數(shù)據(jù)讀取方式(3種)
這篇文章主要介紹了C# OleDbDataReader快速數(shù)據(jù)讀取方式(3種),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
Unity Sockect實(shí)現(xiàn)畫面實(shí)時(shí)傳輸案例原理解析
Socket是比較常用的一種通信方式,本文通過案例給大家介紹Unity Sockect實(shí)現(xiàn)畫面實(shí)時(shí)傳輸功能,感興趣的朋友一起看看吧2021-08-08
C# 計(jì)算標(biāo)準(zhǔn)偏差相當(dāng)于Excel中的STDEV函數(shù)實(shí)例
下面小編就為大家?guī)硪黄狢# 計(jì)算標(biāo)準(zhǔn)偏差相當(dāng)于Excel中的STDEV函數(shù)實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-01-01
C#中圖片旋轉(zhuǎn)和翻轉(zhuǎn)(RotateFlipType)用法分析
這篇文章主要介紹了C#中圖片旋轉(zhuǎn)和翻轉(zhuǎn)(RotateFlipType)用法,實(shí)例分析了C#圖片旋轉(zhuǎn)及翻轉(zhuǎn)Image.RotateFlip方法屬性的常用設(shè)置技巧,需要的朋友可以參考下2015-06-06
C#利用iTextSharp組件給PDF文檔添加圖片/文字水印
這篇文章主要給大家介紹了關(guān)于如何C#利用iTextSharp組件給PDF文檔添加圖片/文字水印的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
C#實(shí)現(xiàn)微信公眾號(hào)群發(fā)消息(解決一天只能發(fā)一次的限制)實(shí)例分享
經(jīng)過幾天研究網(wǎng)上的代碼和謝燦大神的幫忙,今天終于用C#實(shí)現(xiàn)了微信公眾號(hào)群發(fā)消息,現(xiàn)在分享一下2013-09-09

