I'm using a list(of Bomlist) to store a lot of information about individual items and depending on what I'm going to use the list for I'll order it different ways. I'm curious if there is a good/simple way to make a generic order function where I could pass along a parameter(s) for how I'd like to sort everything.
Examples of how I'd like to sort everything could include the below.
Order by partnumber
Order by vendor and then by part number
Order by ReqType with order given, then by vendor and then by part number
The best I could think of would be this, but it's not as dynamic as I'd like and would require additions for new orders.
Public Function BomListOrder(Order as string) as list(of Bomlist)
If (Order = "PN") Then
Return Bomlist.OrderBy(function(x) x.PartNumber).tolist()
Elseif (Order = "PN-Vendor") then
Return Bomlist.OrderBy(function(x) x.PartNumber).thenby(function(x) x.Vendor).tolist()
etc.......
End If
End Function
Full example code
Imports System
Imports System.Collections.Generic
Imports System.Linq
Public Module Module1
Dim BomList As New List(Of BomList)
Public Sub Main()
Bomlist.Add(new bomlist("","5678","Desc","Rev","Designer","B","BBB"))
Bomlist.Add(new bomlist("","dfgh","Desc","Rev","Designer","R","lll"))
Bomlist.Add(new bomlist("","3457","Desc","Rev","Designer","M","MMM"))
Bomlist.Add(new bomlist("","1234","Desc","Rev","Designer","B","NNN"))
Bomlist.Add(new bomlist("","asdf","Desc","Rev","Designer","R","ccc"))
Bomlist.Add(new bomlist("","7896","Desc","Rev","Designer","M","MMM"))
Bomlist.Add(new bomlist("","3698","Desc","Rev","Designer","B","EEE"))
Dim ReqTypeOrder as New List(Of String)({"M", "B", "R"})
For each item in bomlistsort(reqtypeorder)
console.WriteLine(item.PartNumber &" : " & item.ReqType & " : " & item.Vendor)
Next
End Sub
public function BomListSort(ReqTypeOrder as list(of string)) as list(of Bomlist)
return bomlist.OrderBy(function(x) ReqTypeOrder.IndexOf(x.ReqType)).ThenBy(function(x) x.Vendor).ThenBy(function(x) x.PartNumber).ToList()
end function
End Module
public class BomList
Public FilePath As String = "" 'full file name
Public PartNumber As String
Public Description As String
Public Revision As String
Public Designer As String
Public ReqType As String
Public Vendor As String
Public Sub New(FilePath As String, PartNumber As String, Description As String, Revision As String, Designer As String, ReqType As String, Vendor As String)
Me.FilePath = FilePath
Me.PartNumber = PartNumber
Me.Description = Description
Me.Revision = Revision
Me.Designer = Designer
Me.ReqType = ReqType
Me.Vendor = Vendor
End Sub
end class
BomListshould be properties. The good news is that you can change that without changing any code that references them.