-1

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.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
  • What have you tried and where are you stuck? You don't just come to SO and ask for people to do it for you. If you need to sort then learn how to sort. When you think you know what should work then implement it. If what you implement doesn't work, then you have a question to ask here. You can show us what you did and tell us what happened when you did it. – John Jul 01 '22 at 14:48

1 Answers1

0
Dim result = MyList.OrderBy(Function(x) x.PrimaryItem).ThenBy(Function(x) x.SecondaryItem)
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794