ArrayList Sort Error - IComparer did not return zero

  • Thread starter Thread starter RJN
  • Start date Start date
R

RJN

Hi

I have a user defined data type which implements IComparable interface
and has an implementation for the CompareTo method.

Public Class MyDataType Implements IComparable

--Private members
--Public properties

Public Function CompareTo(ByVal obj As Object) As Integer Implements
IComparable.CompareTo
End function

Public Property SortExpression() as String

End property

End Class

I create objects of this type and add it to a arrylist. Next I need to
sort the arraylist on any member of MyDataType. I set the shared
property SortExpression of MydataType to any member that I want to sort
and then call ArrayList.Sort. This works fine. But once I got this
error.

at System.SorterObjectArray.QuickSort(Int32 left, Int32 right)
at System.SorterObjectArray.QuickSort(Int32 left, Int32 right)
at System.Array.Sort(Array keys, Array items, Int32 index, Int32 length,
IComparer comparer)
at System.Collections.ArrayList.Sort(Int32 index, Int32 count, IComparer
comparer)
at System.Collections.ArrayList.Sort()

IComparer (or the IComparable methods it relies upon) did not return
zero when Array.Sort called x. CompareTo(x).

Any help is appreciated

Regards

RJN
 
RJN said:
IComparer (or the IComparable methods it relies upon) did not return
zero when Array.Sort called x. CompareTo(x).

Well, you have one of your custom objects named 'x'. And
the Sort( ) routine compared it to itself, which a quick sort
may occassionally have to do depending on the pivot and the
size of its partitions. And your IComparable said 'x' wasn't
equal to itself.

It's like saying 2 does not equal 2. Sort( ) concluded the
basic laws of Mathematics have come apart and the uni-
verse ends, as far as it knows it.

Make sure that your implementation of CompareTo( )
returns that an object is always equal to itself, by returning
0 when 'obj' equals Me.


Derek Harmon
 
Thanks Derek for your reply.
This is how I'm comparing.

Case "STARTDATE ASC"
CompareTo = StartDate < CType(obj, MyDataType).StartDate
Case "STARTDATE DESC"
CompareTo = StartDate > CType(obj, MyDataType).StartDate

And similarly for other members.

Is there anything wrong here?

Thanks
 
RJN said:
This is how I'm comparing.

Case "STARTDATE ASC"
CompareTo = StartDate < CType(obj, MyDataType).StartDate
Case "STARTDATE DESC"
CompareTo = StartDate > CType(obj, MyDataType).StartDate

And similarly for other members.

Is there anything wrong here?


First, try to avoid using strings for determining what comparison should
be done. (eg, the Case statements) Computers work with numbers, if you
use strings in your tests, there will be overhead involved to convert the
string to numbers that can be compared. Considering you want an entire
array sorted using many calls to this function, you don't want to put rocks
in the path it has to take....

Second, use the data type's own CompareTo methods:

CompareTo = StartDate.CompareTo(Obj)

Or,

CompareTo = DirectCast(obj, MyDataType).StartDate.CompareTo(StartDate)

(The same for your other methods....)

See if that helps
LFS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top