实现多条件下对列表元素进行排序
一、使用LINQ的扩展方法:OrderBy()和ThenBy();
单一条件排序:
list.OrderBy(x=>x.A).ToList(); //根据元素的A属性大小来顺序排序list里的元素;
list.OrderByDescending(x=>x.A).ToList(); //根据元素的A属性大小来降序排序list里的元素;
多排序条件:
list = list.OrderByDescending(x => x.classId)
.ThenByDescending(x => x.id)
.ToList();
先根据元素的classId值排序,classId值相等情况下,再根据id值进行排序;
二、使用C#List的Sort()及其重载方法;
1、单一条件排序
1、对list里的元素进行顺序/升序排序:
list.Sort((x,y)=> x.CompareTo(y));
list.Sort((x,y)=>{return x.CompareTo(y);});
2、对list里的元素进行降序排序:
list.Sort((x,y)=> -x.CompareTo(y));
list.Sort((x,y)=> y.CompareTo(x));
list.Sort((x,y)=>{return y.CompareTo(y);});
3、以list里的元素的某个子属性大小进行比较排