List Multi-Sorting
I have a list that I would like to sort in multiple ways.
My List is made using this:
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));
I start off by getting each line using foreach(var line in theMainList)
.
Now, for each line
I need to compare certain positions and sort them accordingly.
So, FIRST what I would like to compare is each line.Speed
and organize the list nume开发者_运维技巧rically (so if the speeds were 1,2,3,4,5, etc the first line in the list would be the line that has the line.Speed
equal to 1, and then 2, etc.)
SECOND I would like to sort the updated list that is now in order of line.Speed
again. I would like to sort the line.PackageStyle
in the order of this:
"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 I would like to sort the new updated list with the Speed
sorted first, and then thee PackageStyle
sorted secondly...by the line.PartNumber
. Again this would be numerically just like the line.Speed
was.
Is there any way to do this mulitple sorting technique?
Use Linq's OrderBy()
and ThenBy()
methods:
theMainList.OrderBy(l => l.Speed)
.ThenBy(l => l.PackageStyle)
.ThenBy(l => l.PartNumber);
You should be able to accomplish this using the OrderBy
and ThenBy
extension methods available in the System.Linq
namespace. E.g.,
var sortedList = theMainList
.OrderBy(l => l.Speed)
.ThenBy(l => l.PackageStyle)
.ThenBy(l => l.PartNumber);
Keep in mind you might need to override the default comparisons using IComparer. See MSDN for more info.
精彩评论