1. Computing

The IComparable Interface: Using a Custom Sort

Using An Interface, Step by Step

By

Updated May 28, 2010

Using an Interface is a key Object Oriented Programming technique that some people have difficulty picking up. One very useful Interface is the IComparable interface because you can sort anything you can define using it. But coding the interface has been a real struggle for a lot of programmers. This article works through two examples of custom sorts using IComparable to nail down the concepts for you.

First, let's make sure we understand the Sort method that is at the heart of IComparable. Suppose we had an array called CustAccountNum() that we wanted to sort. The solution is really simple. Just use the Sort method of the Array class:


Array.Sort(CustAccountNum)

Just to be complete, this example is coded in the first download. Random six digit account numbers are created and then sorted. (Actually, the code that creates an array of random account numbers is more interesting than sorting them.)

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

Sort is a good example of a "Shared method", sometimes called a "static method". Notice that I didn't "instantiate" anything. I used the Sort method directly out of the Array object and passed the array that we wanted to sort to that object.

There are currently 17 overloads of the Array.Sort method. That's a lot of flexibility. What are all those others? Well, you can use the Generics interface; you can pass a key to Sort; you can sort just part of an array ... Investigating all 17 will be left as an exercise for the reader. Suffice it to say that there are a lot of options.

But it can't do some things. For example, it can't sort "colors".

This is a good time to define just what an Interface is. In concept, it's something like a class, but classes contain operational code; interfaces don't. An Interface only tells you what the class must have but you have to supply the code.

Here's the Microsoft doc on "IComparable interface":

"(The IComparable interface) requires that implementing types define a single method, CompareTo(Object), that indicates whether the position of the current instance in the sort order is before, after, or the same as a second object of the same type. The instance's IComparable implementation is called automatically by methods such as Array.Sort."

Getting back to an array of colors, when you try to sort an array filled with a random assortment of colors, VB throws an InvalidOperationException because it doesn't know how to do that.

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

If you look up the official Microsoft documentation of the String class, you see that it "Implements IComparable" but the documentation of the Color structure doesn't include that specification.

To solve this problem, we could simply use a String array of colors and stuff the Name property into it instead:

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

But that's no fun! Suppose we wanted to sort by, say, the "Blue" component of the color. .NET doesn't have a way of performing that sort either, so let's build one. We do that on the next page!

  1. About.com
  2. Computing
  3. Visual Basic
  4. Using VB.NET
  5. The IComparable Interface: Using a Custom Sort

©2014 About.com. All rights reserved.