views:

35

answers:

1

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
+1  A: 

Don't know if your doing it wrong or not as I don't know what these mile logs are supposed to mean (see my next paragraph). At least I can point out the behavior your seeing. In your first .sort() call, you first compare the first number (let's look at the row in question 2.000 and 20.070). 2.000 is not greater than 20.070 which equates to false, but the OrElse equates to true (9.000 > 6.788), and you return a 1.

Are you sure your input data is OK? I read this as start mile and end mile. How can you start @ mile 20.070 and end on mile 6.788 (Going backwards?). If the data is bad, then your function is probably fine. Else, you probably just want to focus on comparing one column or the other.

Tommy
Yeah, the data is OK - I'm actually updating the program - someone else had gathered the specs (an internal use web app). After talking to the client, apparently going backwards *is* OK, so I'm trying to allow for "reversed" data. So apparently I've just got my logic crossed.
Wayne Werner