Bellow is a sample sorting of an arraylist of integers in ascending order using IComparer:
Public Class Comparer : Implements IComparer
Public Function Compare(ByVal a As Object, ByVal b As Object) As Integer Implements IComparer.Compare
Dim i1 As Integer = CInt(a)
Dim i2 As Integer = CInt(b)
If (i1 < i2) Then
Return -1
End If
If (i1 > i2) Then
Return 1
Else
Return 0
End If
End Function
End Class
In the form where this implementation is to be used, the following shared function is needed:
Public Shared Function Comparer() As IComparer
Return CType(New Comparer(), IComparer)
End Function
So a sample usage of the above Class would be:
Dim aList As New ArrayList
aList.Add(1)
aList.Add(10)
aList.Add(11)
aList.Add(12)
aList.Add(2)
aList.Add(3)
aList.Add(4)
aList.Add(5)
aList.Add(6)
aList.Add(7)
aList.Add(8)
aList.Add(9)
aList.Sort(Comparer)
The ArrayList will now contain the numbers 1 through 12 in ascending order.
For questions, please visit us at www.digioz.com .