如何对有条件的列表进行排序
举个例子:
我有一个 INT 列表
{1,45,63,1,2,100,46,12,2,100}
在代码中:
int[] ints = {1,45,63,1,2,100,46,12,2,100};
Run Code Online (Sandbox Code Playgroud)
我想以两种方式排序 ASC 和 DESC 意思是:
switch (this.SortMode)
{
case ReportSortMode.DESC:
Rank = Rank.OrderByDescending();
break;
case ReportSortMode.ASC:
Rank = Rank.OrderBy();
break;
}
Run Code Online (Sandbox Code Playgroud)
但我需要获取条件值:
升序:{1,1,2,2,12,45,46,63,100,100}
降序:{63,46,45,12,2,2,1,1,100,100}
方法:
Where(x =>x < 100)
可能使用Except类似:
var fakeval = rank.where(x => x<100)
switch (this.SortMode)
{
case ReportSortMode.DESC:
Rank = Rank.OrderByDescending().Except(fakeval);
break;
case ReportSortMode.ASC:
Rank = Rank.OrderBy();
break;
}
//to be full))
public enum ReportSortMode
{
DESC = 1,
ASC = 2,
}
Run Code Online (Sandbox Code Playgroud)
但这没用
对于ASC订单,您无需更改任何内容:
var intsasc = ints.OrderBy(x=>x);
Run Code Online (Sandbox Code Playgroud)
对于DESC顺序,下面的代码应该没问题:
var intsdesc = ints.OrderByDescending(x=>x<100 ? 1 : 0).ThenByDescending(x=>x);
Run Code Online (Sandbox Code Playgroud)
结果:
63
46
45
12
2
2
1
1
100
100
Run Code Online (Sandbox Code Playgroud)