按顺序给出以下值:
011124
01112
011123
1905
我如何使用 Linq 对其进行排序:
List<string> values = new List<string>() { "011124", "01112", "011123", "1905" };
.. 所以实际上是这样的:
List<string> values = new List<string>() { "011124", "011123", "01112", "1905" };
(更新:添加了“1905”,因为之前的演示可以很容易地使用 int 类型进行排序——很抱歉造成混淆)
最佳答案
试试这个片段:
var sortedValues = values
.OrderByDescending(x => x.Length)
.ThenByDescending(x => Convert.ToInt32(x));
如果你真的需要把它作为一个列表来使用,那么在最后添加ToList()
。
关于c# - 如何使用 Linq 按长度对数组进行排序,然后按值排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31245672/