B. Chernick wrote:
>
Now assume that you want to be able to sort the list on multiple
different string and/or integer properties. Does .Net have any
built-in way to do this?
(I should also mention that I've just started reviewing generics for
a cert. I'm not all that familiar with them. Want to be sure I'm not
missing something.)
Generics are the way to do this, and it is not difficult.
In the example class below, I define a Shared test function. This one compares
the integer first; if that is equal, it compares the string. With that in place,
you can use a standard generic list of your class, and the list's built in sort
method.
Public Class SomeClass
Public SomeText As String
Public SomeNumber As Integer
Public Sub New(ByVal Text As String, ByVal Number As Integer)
SomeText = Text
SomeNumber = Number
End Sub
Public Shared Function FancyTest( _
ByVal A As SomeClass, ByVal B As SomeClass) As Integer
FancyTest = A.SomeNumber.CompareTo(B.SomeNumber)
If FancyTest = 0 Then
FancyTest = A.SomeText.CompareTo(B.SomeText)
End If
End Function
End Class
Private Sub Button5_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button5.Click
Dim SomeList As New List(Of SomeClass)
SomeList.Add(New SomeClass("World", 2))
SomeList.Add(New SomeClass("Hello", 1))
SomeList.Add(New SomeClass(", ", 2))
SomeList.Sort(AddressOf SomeClass.FancyTest)
For Each o As SomeClass In SomeList
Debug.Write(o.SomeText)
Next
Debug.Print("")
End Sub