0

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
7
  • For the record, those fields in BomList should be properties. The good news is that you can change that without changing any code that references them. Commented Apr 22 at 2:37
  • Why do you want to order it yourself? If you think that users may want to order in different ways, maybe it would be better to just provide the sequence and let the downstream user order it as desired. Commented Apr 22 at 13:18
  • @jmcilhinney could you explain a little more please, I'm self taught and have likely picked up bad habits from examples I find around. Commented Apr 22 at 13:53
  • @Craig end users will not be sorting anything. This is all for my benefit as I am working on replacing multiple more specific classes with a more generic class that gathers every property and then sorting out what is needed. If I can't figure out a function to dynamically order by different properties, I'll either use the little example function with a string input for the sort order or make a separate function for each different sort order required. Commented Apr 22 at 13:54
  • Data should be exposed publicly pretty much exclusively using properties, while fields should only be used for private data. This is not the place to go into the details of why, as it's already been explained many times in many places. Commented Apr 22 at 17:35

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.