By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,759 Members | 959 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,759 IT Pros & Developers. It's quick & easy.

ArrayList Sort Error - IComparer did not return zero

P: n/a
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
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"RJN" <rj*@yahoo.com> wrote in message news:OJ**************@TK2MSFTNGP12.phx.gbl...
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
Nov 21 '05 #2

P: n/a
RJN
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 21 '05 #3

P: n/a

"RJN" <rj*@yahoo.com> wrote
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

Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.