1. Home
  2. Computing & Technology
  3. Visual Basic

The IComparable Interface: An Intro Using a Custom Sort
The CompareTo Function; The required method in the Interface

By Dan Mabbutt, About.com

Dec 29 2007

We're almost finished, the main task now is to add the code for the CompareTo Function. At this point, it's useful to go back and reconsider how we got here. We're actually coding an implementation of the Sort method of the Array object. When Sort runs, it's going to check whether there is a custom CompareTo Function available. If there is, then Sort runs it. If not, Sort uses a default that simply does an alphabetic comparison. But the main thing that CompareTo has to do is return an integer that is ...

  • Less than zero -- This instance is less than obj.
  • Zero -- This instance is equal to obj.
  • Greater than zero -- This instance is greater than obj.

Here's the code: Public Function CompareTo(ByVal obj As Object) As Integer _
   Implements System.IComparable.CompareTo
   Dim compFH As FHData = CType(obj, FHData)
   ' Debug.Print(compFH.FH & " - " & Me.FH)
   If compFH.FHScore > Me.FHScore Then
      Return 1
   ElseIf compFH.FHScore < Me.FHScore Then
      Return -1
   Else
      Return 0
   End If
End Function

But ... What is "obj"??? (This part is missing from all of Microsoft's documentation and other books I have read too.)

Remember that this function is called by the Sort method of the array. The obj parameter is the "previous" element that Sort needs to compare with the "next" one. Uncomment the Debug statement to see this more clearly. The .NET Sort method uses what is called the "Quicksort" algorithm. You can find lots of references that explain how that works.

The final element is actually using this class that we have written. In addition to declaring an array using the FHData Class, we also need to call the Sort. As it turns out, the same elementary Sort method still works since all of the actual processing happens in the Class code that we wrote.

Array.Sort(HHHarray) ' Call the Sort method

This will do the job of sorting the array, but there is still one job that needs to be done. We also need some way to access the sorted array. This could be done with another method in the FHData Class, but another way to do it is to Override the ToString method so we get a custom display of the data as well. Here's some code that will do that job.

Public Overrides Function ToString() As String
   Return "Score: " & FHScore & " - " & _
   FH & " " & _
   FHHasCar & " a car and has " & _
   FHHasMoney & " money. He's " & _
   FHIsHunkOrJunk & "."
End Function

Click here to download the complete HHHReport program.

Explore Visual Basic
By Category
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Using VB.NET
  5. The IComparable Interface: An Intro Using a Custom Sort

©2009 About.com, a part of The New York Times Company.

All rights reserved.