0

如何按 ASC 顺序排序?

我得到了List<List<UInt32>>完整的记录,List<UInt32>我希望这些记录根据每条记录的第一列进行排序 - 该列是 UInt32 编号。

所以:

我有以下清单:

new List<UInt32>: 32,1,1,1,1,1
new List<UInt32>: 22,2,2,2,2,2
new List<UInt32>: 32,1,1,1,1,1
new List<UInt32>: 1,1,1,1,1

应排序为:

List<UInt32>: 1,1,1,1,1
List<UInt32>: 22,2,2,2,2,2
new List<UInt32>: 32,1,1,1,1,1
new List<UInt32>: 32,1,1,1,1,1

-> 只有第一个数字很重要!其他对于排序并不重要。我正在寻找 ASC 的东西,但两者都很棒,所以当我认为应该更改算法时,我会查一下 :)

感谢您的努力帮助!

@Samuel,谢谢,我会在尝试更改类型时尝试实现它:)

4

2 回答 2

5

几乎重复:

如何根据 List<string> 字段数对 List<List<string>> 进行排序?

就像是:

var sorted = parent.OrderBy( l => l[0] );

或者:

parent.Sort( (a,b) => a[0].CompareTo( b[0] ) )
于 2009-04-07T13:21:27.387 回答
3

虽然约翰的回答有效,但我建议使用First()而不是访问列表中的 0 元素。这样,它不仅适用于IEnumerable<T>任何IList<T>.

var sorted = list.OrderBy(subList => subList.First());
于 2009-04-07T13:33:52.113 回答