List<T>多字段排序的一个通用类
本文中的方法旨在解决通用的问题,非常注重效率的地方,还应该针对具体的类去写排序方法。
废话不多说,直接上代码
具体使用场景:
- 要排序的类
1 public class bb
2 {
3 public aa a { get; set; }
4 }
5
6 public class aa
7 {
8 public string index { get; set; }
9 public string name { get; set; }
10
11 public aa(string index, string name)
12 {
13 this.index = index;
14 this.name = name;
15 }
16 }
- 使用CommentComparer类排序
1 List<bb> list = new List<bb>()
2 {
3 new bb(){a = new aa("13", "35")},
4 new bb(){a = new aa("14", "35")},
5 new bb(){a = new aa("12", "35")},
6 new bb(){a = new aa("14", "12")},
7 };
8
9 CommentComparer<bb> comparer = new CommentComparer<bb>();
10 //comparer.IsAsc = true;
11 //comparer.IsProperty = true;
12 comparer.AddField("a.index");
13 comparer.AddField("a.name");
14
15 list.Sort(comparer);
- 结果
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAW8AAAGYCAIAAAACyfTiAAAgAElEQVR4nO2dvW7cOt9vdSm5hw2kmNp9OhcPXDiNgjc3sUsDrgbpdnGAMVIaaacKEgSY2m2KCQJsnDQHAXKe0h3fQiOJH39SooZjkdJamMJR9DmS1pAU+VP1X4ffv//83//3//nw4cMn6lO5NgEAmAA2AYA0YBMASAM2AYA0YBMASAM2AYA0eG3ydPf69rH71+Pt67unF9khACgUn02e7l7fPj7eViKaZgAAGjw2MUsmvrLJ093rqnGO6Zenu9fV67u724oCDcB6kG3ydPf69d1TowWbzhDtTP+1ddJIRp8DAJaPaJPH25M0HB1oEx5vO4WYOulkYs4EAMtGsMnT3evXrwfLJoZodJ3oMunnaio/lFMAlotgk8e7u8e+puMrm5iljl4hhkz62bAJwNKZ2m5ie6aViCUTmk4AVsOgTcaVTVqN2DKh4QRgNUx+pvNoP/99untdte0txkRqOgCrYHLZxC11PN29rmxjdDNhE4ClE7DJnacnrK/pxLMaDAKwDs4a9ee0khg4dSEAWDKMIQaANGATAEgDNgGANGATAEgDNgGANGATAEiDYJMdAEA8sk3+BwAgE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AAAABJRU5ErkJggg==" alt="" />
CommentComparer类具体实现
/// <summary>
/// 排序类
/// </summary>
/// <typeparam name="T"></typeparam>
public class CommentComparer<T> : IComparer<T>
{
/// <summary>
/// 字段列表
/// </summary>
private List<string> fieldList; /// <summary>
/// 排序字段序号
/// </summary>
private int index = ; /// <summary>
/// 正序
/// </summary>
public bool IsAsc { get; set; } /// <summary>
/// 使用字段的属性排序
/// <para>False:使用字段排序</para>
/// </summary>
public bool IsProperty { get; set; } public CommentComparer()
{
fieldList = new List<string>();
IsAsc = true;
IsProperty = true;
index = ;
} /// <summary>
/// 添加排序使用的字段
/// </summary>
/// <param name="fieldName"></param>
public void AddField(string fieldName)
{
fieldList.Add(fieldName);
} public int Compare(T x, T y)
{
if (x == null)
{
if (y == null)
{
return ;
} return ; }
if (y == null)
{
return -;
} int result = -; if (fieldList.Count < )
{ throw new FormatException("请添加排序所使用的字段!"); }
if (index == && !IsAsc)
{
T temp;
temp = y;
y = x;
x = temp;
} string[] field = fieldList[index].Split('.'); dynamic p1 = GetValue<T>(x, field, );
dynamic p2 = GetValue<T>(y, field, ); result = Comparer<string>.Default.Compare(p1, p2);
if (result == )
{
index++;
if (fieldList.Count > index)
{ result = Compare(x, y); }
index--;
}
return result;
} /// <summary>
/// 获取属性值
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <param name="field"></param>
/// <param name="i"></param>
/// <returns></returns>
private dynamic GetValue<T>(T obj, string[] field, int i)
{
dynamic result; Type t = typeof(T); if (IsProperty)
{
PropertyInfo propertyInfo = t.GetProperty(field[i]);
result = propertyInfo.GetValue(obj, null);
}
else
{
FieldInfo fieldInfo = t.GetField(field[i]);
result = fieldInfo.GetValue(obj);
} if (i < field.Length - )
{
result = GetValue(result, field, ++i);
}
return result;
}
}
List<T>多字段排序的一个通用类的更多相关文章
- 根据某个字段去重 根据另一个字段排序的一个SQL
背景:一张表 有 name , crt_time , work_sts 三个字段 这张表里存在这样的数据 'a', '2018-12-18 21:37:24', '未''a', '2018-12-19 ...
- sql 多个字段排序,头一个字段排序完,再对第二个字段进行排序(以此类推)
现根据num排序,num数字相同的根据时间进行排序,都是降序DESC SELECT * FROM counts ORDER BY num DESC,create_time DESC
- .NET基础篇——Entity Framework 数据转换层通用类
在实现基础的三层开发的时候,大家时常会在数据层对每个实体进行CRUD的操作,其中存在相当多的重复代码.为了减少重复代码的出现,通常都会定义一个共用类,实现相似的操作,下面为大家介绍一下Entity F ...
- php 数组排序以及按照某个字段排序
经常,开发人员发现在PHP中使用这种数据结构对值或者数组元素进行排序非常有用.PHP提供了一些适合多种数组的排序函数,这些函数允许你在数组内部对元素进行排列,也允许用很多不同的方法对它们进行重新排序. ...
- PHP 二维数组根据某个字段排序
二维数组根据某个字段排序有两种办法,一种是通过sort自己写代码,一种是直接用array_multisort排序函数 一. 手写arraysort PHP的一维数组排序函数: sort 对数组的值按 ...
- 对文本行按特定字段排序(前N个字符或后N个字符),TCPL 练习5-17
The C programming language 的关于文本行排序的问题有很多种要求的方式,在对每行的字段排序方面,最简单的是例如对前N个字符或者末位N个字符进行排序,更高一点的要求是,对特殊符号 ...
- 一个通用的DataGridView导出Excel扩展方法(支持列数据格式化)
假如数据库表中某个字段存放的值“1”和“0”分别代表“是”和“否”,要在DataGridView中显示“是”和“否”,一般用两种方法,一种是在sql中直接判断获取,另一种是在DataGridView的 ...
- 利用RBAC模型实现一个通用的权限管理系统
本文主要描述一个通用的权限系统实现思路与过程.也是对此次制作权限管理模块的总结. 制作此系统的初衷是为了让这个权限系统得以“通用”.就是生产一个web系统通过调用这个权限系统(生成的dll文件), 就 ...
- 我写的一个ExcelHelper通用类,可用于读取或生成数据
读取或生成EXCEL数据的方法有很多,一般常见的有: 1.通过OFFICE EXCEL组件,优点:读取与生成EXCEL文件方便,缺点:服务器上必须安装OFFICE软件,且进程无法及时释放 2.通过第三 ...
随机推荐
- 【codeforces 752F】Santa Clauses and a Soccer Championship
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- iOS 一个简单的单例
比如我有一个Singleton的类(DemoStatusManage),他有一个实例方法currentStatus会返回一个1-100的随机数. @interface DemoStatusManage ...
- 【t061】游览路线
Time Limit: 1 second Memory Limit: 128 MB [问题描述] 话说LCINF信息组来到烟台参加夏令营.一天,大家提议出去游玩,来到了烟台最繁华的地方.由于他们对烟台 ...
- const常量用extern声明定义的问题(extern变量不能在使用类里初始化)
test.h #ifndef TEST_H_ #define TEST_H //常量声明和定义采取这种方法即可 const int a = 20; //不报错,因为const变量链接属性默认是内部链 ...
- 【13.91%】【codeforces 593D】Happy Tree Party
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- lucene 7.x 排序
一.创建索引 @Test public void indexCreate() throws IOException { //创建分词器 Analyzer analyzer = new Standard ...
- 分位数和分位线(Quantiles and Percentiles)
分位数有种积分(累积)的含义在. 分位数(即将数据由低至高排列,小于该数的数据占总体的比例达到时最终落到的数): 10%:3000元 20%:5200元 50%:20000元 80%:41500元 9 ...
- 可以左右移动横向无缝滚动的JS图片展示代码
在酷站网站下的,具体路径忘了,稍微改了一下,让它看起来像组滑动 1)被引用的js文件ScrollPic.js ?){){i+=l.length;)I=document.cookie.length;o= ...
- 解决:insert Vodafone sim card,open the mms read report,when receive the read report,cann't download..
insert Vodafone sim card,open the mms read report,when receive the read report,cann't download the m ...
- hx计算机基础
参考:http://python.jobbole.com/82294/ https://www.jianshu.com/p/aed6067eeac9 1. 操作系统基础题 1)在32位操作系统下,系统 ...