简体   繁体   English

如何在C#中排序列表列表

[英]How can I order a list of lists of lists in C#

I'm actually using a list which contains lists which contains lists (yes three sets): 我实际上正在使用一个包含包含列表的列表的列表(是三组):

List<List<List<int>>> threeD = new List<List<List<int>>>();

after I fill this monster, I get this: 填满这个怪物之后,我得到了:

var threeD = [ [[18,100],[13,95]], [[12,100],[2,5],[7,45]], [[19,100]] ];

The above list contains three separate lists with a list with an x and y int. 上面的列表包含三个单独的列表,并带有一个x和y int的列表。 How can I sort these based on the first number. 如何根据第一个数字对它们进行排序。 My output I would need would be this: 我需要的输出是这样的:

var threeD = [ [[13,95],[18,100]], [[2,5],[7,45],[12,100]], [[19,100]] ];

I was thinking of maybe sorting using some kind of linq statement. 我当时正在考虑使用某种linq语句进行排序。 Or should I maybe proceed by creating nested for loops? 还是应该通过创建嵌套的for循环来继续?

Edit: I only need to sort the inner-most lists and not the outer lists. 编辑:我只需要排序最里面的列表,而不是外面的列表。 They are actually cordinates for a line graph with multiple lines using jqplot so the order that each line is rendered doesn't really matter. 实际上,它们是使用jqplot包含多条线的线图的坐标,因此呈现每条线的顺序并不重要。 Thanks 谢谢

You first sort the inner list of x/y numbers, and then you order the outer list: 首先对x / y数字的内部列表进行排序,然后对外部列表进行排序:

threeD.Select(sublist => sublist.OrderBy(xy => xy[0]).ToList())
      .OrderBy(sublist => sublist[0][0])
      .ToList();

And if you just need to sort the inner list, just leave the outer sort out: 如果只需要对内部列表进行排序,则只需将外部列表排除在外即可:

threeD.Select(sublist => sublist.OrderBy(xy => xy[0]).ToList()).ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM