The more difficult part of the problem is to sort the array according to a complex set of relationships. One way to go about this would be to calculate a FHScore when the data is entered and then sort the array using this score as a sort key. Simply concatenating the key to the front of the string of factors and then using the most elementary Sort method of the Array object would do the job:
Dim HHHarray() As String = { _
("30" & _
"Delbert" & _
"has" & _
"huge piles of" & _
"junk! If he calls, tell him you died"), _
("10" & _
"Bertram" & _
"has" & _
"almost no" & _
"someone to introduce to your cousin"), _
("20" & _
"Egmont" & _
"doesn't have" & _
"a nice wad of" & _
"someone to avoid at lunch") _
}
Array.Sort(HHHarray) ' Call the Sort method
For i As Short = 0 To HHHarray.Length - 1
Debug.Print(HHHarray(i))
Next i
But we can do much better than that. There are (in .NET 2.0) 16 overloads of the Sort method. That's a lot of flexibility.
Ruth, a computer scientist, has decided that she wants a design that keeps the data totally separate from the calculation. So we will use a method that calls our own code as the array is sorted.
The key to doing this is to implement the IComparable Interface in some custom code that is called by the Sort method.
If you read the Microsoft documentation for the Sort method, you will see language like this:
"Sorts the elements in an entire one-dimensional Array using the IComparable implementation of each element of the Array."
The "take away" fact here is that IComparable is part of the implementation of the elements of the array itself. So we need more than just a Dim statement to declare the array. What we actually need is a new Class. This new class will contain not only our data, but also the custom calculation that will be called when the array is sorted.
This is a good time to define just what an Interface is. It's a type in .NET that requires the code that implements the Interface to also implement specific methods along with it. In concept, it's something like a Class, but Classes contain code; Interfaces don't. The Interface just tells you what code you have to have when you implement it. The particular Interface we will be using, the IComparable Interface, (An Interface traditionally has a name starting with the letter I) requires a Function called CompareTo.
You can certainly define your own Interface, but as you get started using this technique, you'll probably just use one that is part of .NET.
Now back to solving Ruth's problem. Our "Future Husband" data will be contained in instances of a new Class that we will define that implements the IComparable Interface. Our Class is declared this way:
Private Class FHData
Implements IComparable
...
End Class
Notice that when you type this into Visual Studio, it helpfully adds:
Public Function CompareTo( _
ByVal obj As Object) _
As Integer _
Implements System.IComparable.CompareTo
End Function
That's the required method for the Interface. In words, there is a CompareTo Function defined in the IComparable Interface, but no code. You have to add the code when your Class Implements of that Function. That's what we'll do next.
When we create New elements of the array, we will be creating new instances of the FHData Class. You saw that to begin with at the top of this article.
The internal values of all those descriptions will be part of each instance of the class, along with FHScore, the calculated score for each instance.
Protected FHScore As Integer
Protected FH As String
Protected FHHasCar As String
Protected FHHasMoney As String
Protected FHIsHunkOrJunk As String
We will need a New method in the class (called the "constructor") to handle this job. Here's the code for that:
Public Sub New( _
ByVal theFH As String, _
ByVal theFHHasCar As String, _
ByVal theFHHasMoney As String, _
ByVal theFHIsHunkOrJunk As String)
We're getting close to the finish line. On the next page, the custom calculation used in sorting will be inserted into the program.

