How to sort a custom list - SecondaryItem within PrimaryItem

28 Views Asked by At

Hi there have a list of custom objects that I need to be able to sort, one property within another. How do I go about doing this using .Net. Normally I would carry out all sorting requirements within the SQL that delivers the data, unfortunately in this case I don't have control over that generation of the raw data. I consequently have no experience of sorting content using .Net functions such as Linq and/or IComparable. An impression of the code elements involved are listed below:

 Public Class CustomObject

      Public Property PrimaryItem As String
           Get
                Return _primaryItem
           End Get
           Set(ByVal value As String)
                _primaryItem = value
           End Set
      End Property

      Public Property SecondaryItem As String
           Get
                Return _secondaryItem
           End Get
           Set(ByVal value As String)
                _secondaryItem = value
           End Set
      End Property

 End Class

 Public Class CustomObjectList
      Inherits List(Of CustomObject)

      Public Sub New()
      End Sub
 End Class

In essence I want to be able to sort CustomObjectList according to SecondaryItem within PrimaryItem:

PrimaryItem1
     SecondaryItem1
     SecondaryItem2
     etc...
PrimaryItem2
     SecondaryItem1
     SecondaryItem2
     etc...
etc...

Hoping that some kind person will be able to give me a 'leg up' in usage of either Linq and/or iComparable.

1

There are 1 best solutions below

0
Joel Coehoorn On
Dim result = MyList.OrderBy(Function(x) x.PrimaryItem).ThenBy(Function(x) x.SecondaryItem)