C# 列表多重排序

C# 列表多重排序,c#,list,sorting,C#,List,Sorting,我有一个列表,我想在多种方式排序 我的列表是使用以下内容编制的: theMainList.Add(new LoadLine(theChipList[count].Name, theChipList[count].PartNumber, theChipList[count].XPlacement, theChipList[count].YPlacement, theChipList[count].Rotation, theChip

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

我的列表是使用以下内容编制的:

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行)
获取每一行

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

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

第二次我想再次按照
行的顺序对更新后的列表进行排序。我想按以下顺序对
行.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"
第三个我想对新更新的列表进行排序,首先排序的是
速度
,然后是
包装样式
第二个排序…按
行。零件号
。同样,这在数字上与
行相似。速度是

有什么方法可以实现这种多重排序技术吗?

使用Linq和方法:


您应该能够使用
System.Linq
命名空间中提供的
OrderBy
ThenBy
扩展方法来完成这一任务。例如:

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

请记住,您可能需要使用IComparer覆盖默认比较。有关更多信息,请参阅。

@Iasaac:这似乎没有正确排序。。我有一个绑定到列表的DataGridView,当我更新列表?已更新时,它没有对DataGridView进行排序
OrderBy
不会对列表进行操作,它返回一个表示已排序列表的
IOrderedEnumerable
。我尝试添加此项以设置DGV的数据源:
mainDGV.DataSource=sortedList?然而,这似乎不起作用。。我是否遗漏了需要添加的其他内容?这似乎没有正确排序。。我将一个DataGridView绑定到一个列表,当我更新列表时,它没有对DataGridView进行排序?@theNoobGuy-这些方法返回一个排序的列表,它们不会对原始列表进行排序。您需要将DataGridView重新绑定到已排序列表。。或者我还需要做些什么,因为这似乎无法正常工作。@theNoobGuy-在将排序列表设置为数据源后,请记住还要执行
mainDGV.DataBind()
。上下文是什么?这是Windows窗体吗?ASP.Net?
var sortedList = theMainList
   .OrderBy(l => l.Speed)
   .ThenBy(l => l.PackageStyle)
   .ThenBy(l => l.PartNumber);