c#集合快速排序类实现代码分享
发布时间:2023-03-30 13:38:51
说明:
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
}


猜你喜欢
- IDEA maven项目中刷新依赖的方法IDEA maven项目中刷新依赖分为自动刷新 和 手动刷新 两种!自动刷新File-Setting
- 1 场景调用多个平级服务,按照服务优先级返回第一个有效数据。具体case:一个页面可能有很多的弹窗,弹窗之间又有优先级。每次只需要返回第一个
- 我在 android里面 使用html5的 localStorage 为什么存不进去也读不出来呀?网上搜了好多都没效果mainWebView
- 概念理解Properties 继承于 Hashtable。表示一个持久的属性集,属性列表以key-value的形式存在,key和value都
- 1.理解装箱简单地说,装箱就是将一个值类型的数据存储在一个引用类型的变量中。假设你一个方法中创建了一个 int 类型的本地变量,你要将这个值
- 随着互联网公司的微服务越来越多,分布式事务已经成为了我们的经常使用的。所以我们来一步一步的实现基于RocketMQ的分布式事务。接下来,我们
- 前言最近因为项目组需求,特研究了一下“回到顶部”效果,即:页面里有scrollview,内容很多,当滑动到页面下面或者更深时,需要回到顶部,
- 前言泛型,一个孤独的守门者。大家可能会有疑问,我为什么叫做泛型是一个守门者。这其实是我个人的看法而已,我的意思是说泛型没有其看起来那么深不可
- 问题原因今天在看集合源码的时候,突然看到接口继承接口,觉得有点差异,以前写代码也就是类继承一个类,实现接口。这样写的多了,突然看到接口继承接
- 关于注入数据说明1.不通过配置文件注入数据通过@Value将外部的值动态注入到Bean中,使用的情况有:注入普通字符串注入操作系统属性注入表
- 依赖如下:<dependency> <groupId>org.springframework.boot&
- 刚开始项目,需要用到mybatis分页,网上看了很多插件,其实实现原理基本都大同小异,但是大部分都只给了代码,注释不全,所以参考了很多篇文章
- 什么是Flyweight模式?享元模式(Flyweight Pattern)是一种软件开发中的设计模式,其主要解决的问题是通过类对象的共享,
- 今天遇到这样一个bug:客户端POST到服务器的一段数据导致服务器端发生未知异常。服务器端确认是编码转换错误。于是截取网络数据包进行分析,发
- 目录背景Shutdown Hook 介绍关闭钩子被调用场景注意事项实践Shutdown Hook 在 Spring 中的运用背景如果想在 J
- 网络唤醒实现了对网络的集中管理,即在任何时刻,网管中心的IT管理人员可以经由网络远程唤醒一台处于休眠或关机状态的计算机。使用这一功能,IT管
- 一、启动android默认浏览器这样子,android就可以调用起手机默认的浏览器访问。二、指定相应的浏览器访问1、指定android自带的
- 目录实现方法问题在出问题的设备上,使用简单的Show()、Active()方法激活窗口是不行的,只会在任务栏闪烁图标,使用如下方法可以激活实
- 前言:如果简单地拍照片并非您应用的主要目标,那么您可能希望从相机应用中获取图片并对该图片执行一些操作。一、这就是第一种方法,比较简单,不用将
- SpringBoot web项目启动后立即关闭我们在写spring boot web项目时,有时会遇到启动后立即关闭的情况,或者是无法加载某