您的位置:首页 > 编程语言 > C#

C# 集合扩展快速排序算法

2015-06-09 21:42 363 查看
/// <summary>
/// 对集合进行排序,如
/// List<Person> users=new List<Person>(){.......}
/// ListSorter.SortList<list<Person>,Person>( users,"Age",SortDirection.Ascending);
/// </summary>
public static class ListUtil
{
public static void SortList<TCollection, TItem>(this 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>(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>(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>(list, left, i - 1, propertyinfo, direction);
QuickSort<TCollection, TItem>(list, i + 1, right, propertyinfo, direction);
}
}
}
/// <summary>
/// 排序类型
/// </summary>
public enum SortDirection
{
Ascending,
Descending
}


static void SortList<TCollection, TItem>(params...)

static void QuickSort<TCollection, TItem>(params...)

两个函数摘自http://www.cnblogs.com/sosoft博客,我直接将算法扩展给了List<T>类

测试:

static void  Main(){
Console.WriteLine("\r\n=======List<T>快速排序========");
List<Person> list2 = new List<Person>()
{
new Person { Name = "1", Age = 1 },
new Person { Name = "2", Age = 2 },
new Person { Name = "20", Age = 20 },
new Person { Name = "200", Age = 200 },
new Person { Name = "-1", Age = -1 },
new Person { Name = "-2", Age = -2 },
new Person { Name = "0", Age = 0 },
};

list2.SortList<List<Person>, Person>("Age", SortDirection.Descending);

foreach (var temp in list2)
{
Console.Write(temp.Name + " ");
}
Console.ReadLine();
}

public  class Person
{
public string Name { get; set; }

public int Age { get; set; }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: