Linq OrderBy against specific values

2019-01-13 17:57发布

Is there a way in Linq to do an OrderBy against a set of values (strings in this case) without knowing the order of the values?

Consider this data:

A
B
A
C
B
C
D
E

And these variables:

string firstPref, secondPref, thirdPref;

When the values are set like so:

firstPref = 'A';
secondPref = 'B';
thirdPref = 'C';

Is it possible to order the data like so:

A
A
B
B
C
C
D
E

7条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-01-13 18:29

Yes, you must implement your own IComparer<string> and then pass it in as the second argument of LINQ's OrderBy method.

An example can be found here: Ordering LINQ results

查看更多
登录 后发表回答