title: Sort List(of Object) by object properties
body: <p>I'm trying to achieve something where the answer is already given for. But it's in <code>c#</code> and I don't have any knowledge what-so-ever over <code>c#</code> so I'm looking for a vb.net alternative.</p>
  
  <p>I made a <code>class</code> called <code>BomItem</code> which has several properties like quantity, description etc.</p>
  
  <p>I add these <code>BomItems</code> into a <code>List(of BomItem)</code> but now I would like to sort them according to a property. How can you sort the items based on the <code>ItemNumber</code> property?</p>
  
  <p>Here is the <a href="https://stackoverflow.com/questions/3309188/how-to-sort-a-listt-by-a-property-in-the-object">link</a> to the <code>c#</code> solution I found.</p>
  
  <p>My class code</p>
  
  <pre><code>Public Class BomItem
      Public Property ItemNumber As String
      Public Property Description As String
      Public Property Quantity As Double
      Public Property Material As String
      Public Property Certificate As String
  End Class
  </code></pre>
  
  <p>How I add the <code>BomRow</code> objects</p>
  
  <pre><code>    _NewBomList.Add(New BomItem() With {
                      .ItemNumber = oRow.ItemNumber,
                      .Description = oPropSet.Item("Description").Value,
                      .Quantity = oRow.TotalQuantity,
                      .Material = oPropSet.Item("Material").Value,
                      .Certificate = CustomPropertySet.Item("Cert.").Value})
  </code></pre>
  
  <p>Comparer</p>
  
  <pre><code>Public Class NaturalSort
  
  Implements IComparer
  
  Public Function Compare(ByVal x As Object,
              ByVal y As Object) As Integer Implements IComparer.Compare
  
      ' [1] Validate the arguments.
      Dim s1 As String = x
      If s1 = Nothing Then
          Return 0
      End If
  
      Dim s2 As String = y
      If s2 = Nothing Then
          Return 0
      End If
  
      Dim len1 As Integer = s1.Length
      Dim len2 As Integer = s2.Length
      Dim marker1 As Integer = 0
      Dim marker2 As Integer = 0
  
      ' [2] Loop over both Strings.
      While marker1 &lt; len1 And marker2 &lt; len2
  
          ' [3] Get Chars.
          Dim ch1 As Char = s1(marker1)
          Dim ch2 As Char = s2(marker2)
  
          Dim space1(len1) As Char
          Dim loc1 As Integer = 0
          Dim space2(len2) As Char
          Dim loc2 As Integer = 0
  
          ' [4] Collect digits for String one.
          Do
              space1(loc1) = ch1
              loc1 += 1
              marker1 += 1
  
              If marker1 &lt; len1 Then
                  ch1 = s1(marker1)
              Else
                  Exit Do
              End If
          Loop While Char.IsDigit(ch1) = Char.IsDigit(space1(0))
  
          ' [5] Collect digits for String two.
          Do
              space2(loc2) = ch2
              loc2 += 1
              marker2 += 1
  
              If marker2 &lt; len2 Then
                  ch2 = s2(marker2)
              Else
                  Exit Do
              End If
          Loop While Char.IsDigit(ch2) = Char.IsDigit(space2(0))
  
          ' [6] Convert to Strings.
          Dim str1 = New String(space1)
          Dim str2 = New String(space2)
  
          ' [7] Parse Strings into Integers.
          Dim result As Integer
          If Char.IsDigit(space1(0)) And Char.IsDigit(space2(0)) Then
              Dim thisNumericChunk = Integer.Parse(str1)
              Dim thatNumericChunk = Integer.Parse(str2)
              result = thisNumericChunk.CompareTo(thatNumericChunk)
          Else
              result = str1.CompareTo(str2)
          End If
  
          ' [8] Return result if not equal.
          If Not result = 0 Then
              Return result
          End If
      End While
  
      ' [9] Compare lengths.
      Return len1 - len2
  
  End Function
  
  End Class
  </code></pre>