Hi,
I've got a class with the following overload:
Public Overloads Function CompareTo(ByVal obj As Object) As Integer Implements IComparable.CompareTo
' I want a logmile, darn it!
If Not TypeOf obj Is Logmile Then Throw New ArgumentException
If Me("beg_logmile") > obj("beg_logmile") OrElse Me("end_logmile") > obj("end_logmile") Then
' I start or end after the other guy
Return 1
ElseIf Me("beg_logmile") < obj("beg_logmile") OrElse Me("end_logmile") < obj("end_logmile") Then
' I start or end before the other guy
return -1
Else
' Well, we must be equivalent then!
Return 0
End If
End Function
I create an ArrayList of the following data (not ordered at the time it's input):
0.000 1.000 1.000 2.000 2.000 9.000 20.070 6.788
then I call the .sort() method of the ArrayList containing my objects. The output I get is
0.000 1.000 1.000 2.000 20.070 6.788 2.000 9.000
However, if in my compareTo
I swap -1 and 1, I get the following output:
20.070 6.788 2.000 9.000 1.000 2.000 0.000 1.000
Voila! It's ordered in the way I expect! Just in reverse.
So here's my question: Have I discovered a bug or am I just not doing it right?
Thanks
EDIT:
Conclusion - My logic was just written wrong.
Updated logic:
If Me("beg_logmile") > obj("beg_logmile") OrElse Me("beg_logmile") = obj("beg_logmile") and Me("end_logmile") > obj("end_logmile") Then
' I start or end after the other guy
Return 1
ElseIf Me("beg_logmile") < obj("beg_logmile") OrElse Me("end_logmile") = obj("end_logmile") and Me("end_logmile") < obj("end_logmile") Then
' I start or end before the other guy
Return -1
Else
' Well, we must be equivalent then!
Return 0
End If