List Multi-Sorting

本文关键字:Multi-Sorting List | 更新日期: 2023-09-27 18:06:41

我有一个列表,我想用多种方式排序。

My List是用这样创建的:

theMainList.Add(new LoadLine(theChipList[count].Name, theChipList[count].PartNumber, 
                theChipList[count].XPlacement, theChipList[count].YPlacement, 
                theChipList[count].Rotation, theChipList[count].PkgStyle, 
                theChipList[count].PackageType, theChipList[count].PartDescription,
                theChipList[count].Feeder, theChipList[count].Vision,
                theChipList[count].Speed, theChipList[count].Machine,
                theChipList[count].TapeWidth, theChipList[count].PlacingTime));

我首先使用foreach(var line in theMainList)获取每行。

现在,对于每个line,我需要比较某些位置并相应地对它们进行排序。

因此,FIRST我想比较的是每个line.Speed并以数字形式组织列表(因此,如果速度为1,2,3,4,5等,列表中的第一行将是line.Speed等于1的行,然后是2,等等)

SECOND我想再次按line.Speed的顺序对更新后的列表进行排序。我想按以下顺序对line.PackageStyle进行排序:

"FIDUCIAL", "FID", "FID0", "FID1", "FID2", "FID3", "FID4", "FID5",
"FID6", "FID7", "FID8", "FID9", "RES", "0402", "0201", "0603", 
"0805","1206", "1306", "1608", "3216", "2551", "1913", "1313",
"2513","5125", "2525", "5619", "3813", "1508", "6431", "2512",
"1505","2208", "1005", "1010", "2010", "0505", "0705", "1020",
"1812","2225", "5764", "4532", "1210", "0816", "0363", "SOT"

THIRD我想对新更新的列表进行排序,Speed首先排序,然后三个PackageStyle其次排序…由line.PartNumber。同样,这将是一个数字,就像line.Speed一样。

是否有办法实现这种多重排序技术?

List Multi-Sorting

使用Linq的OrderBy()ThenBy()方法:

theMainList.OrderBy(l => l.Speed)
           .ThenBy(l => l.PackageStyle)
           .ThenBy(l => l.PartNumber);

您应该能够使用System.Linq名称空间中可用的OrderByThenBy扩展方法来完成此操作。例如,

var sortedList = theMainList
   .OrderBy(l => l.Speed)
   .ThenBy(l => l.PackageStyle)
   .ThenBy(l => l.PartNumber);

请记住,您可能需要使用IComparer覆盖默认比较。

相关文章: