Wednesday, May 03, 2006

Needed to sort an array of objects. The code from here was very useful http://www.vbcity.com/forums/topic.asp?tid=36084 basically had to implement Icomparable inside the object you wanted to sort.

We will set this class to sort by the FullName property in ascending order.

Code:
Public Function CompareTo(ByVal obj As Object) As Integer Implements System.IComparable.CompareTo
Dim p As Person

' Any non-Nothing object is greater than nothing.
If obj Is Nothing Then
Return 1
End If

' Avoid late-binding and cast to a specific Person Object.
p = CType(obj, Person)

' Use the String's CompareTo Method to perform the check.
Return Me.FullName.CompareTo(p.FullName)

End Function

No comments: