Linq101 範例
1 | private class CaseInsensitiveComparer : IComparer<string> |
輸出結果:1
2
3
4
5
6AbAcUs
aPPLE
BlUeBeRrY
bRaNcH
cHeRry
ClOvEr
那如果我要把某個特定的條件擺放在最前面的話
1 | static void Main(string[] args) |
另一個範例1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35public class JJAlwaysOnTop:IComparer<string>
{
public int Compare(string s1, string s2)
{
if (s1.Equals(s2)) return 0;
if ("JJ".Equals(s1)) return -1;
else if ("JJ".Equals(s2)) return 1;
return s1.CompareTo(s2);
}
}
void Main()
{
string[] its = {"KK", "RR", "FF", "TT", "CC", "JJ"};
JJAlwaysOnTop comparer = new JJAlwaysOnTop();
var query = its.OrderBy (i => i);
query.Dump("預設排序結果");
query = its.OrderBy(i => i, comparer);
query.Dump("自訂排新結果");
}
//預設排序 輸出結果:
//C
//FF
//JJ
//KK
//RR
//TT
//自訂排序 輸出結果:
//JJ
//CC
//FF
//KK
//RR
//TT