473,395 Members | 1,637 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

ArrayList Sort Error - IComparer did not return zero

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
3 10258
"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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Adam J. Schaff | last post by:
Hello. I recently noticed that the Sort method of the .NET ArrayList class does not behave as I expected. I expect 'A' < '_' < 'a' (as per their ascii values) but what I got was the opposite....
19
by: Derek Martin | last post by:
Hi there, I have been playing with sorting my arraylist and having some troubles. Maybe just going about it wrong. My arraylist contains objects and one of the members of the object is 'name.' I...
9
by: Paul Nations | last post by:
I've got arraylists of simple classes bound to controls. I need to search through those arraylists to set the correct SelectedItem in the control. The code looks like: Public Class...
0
by: Grant Wickman | last post by:
Our team has just fixed a really nasty problem that appears to be caused by an obscure bug in the sort method of Arraylist and daisy-chained webservice stubs. We've fixed the bug so I'll not...
16
by: RCS | last post by:
So I have an ArrayList that gets populated with objects like: myAL.Add(new CustomObject(parm1,parm2)); I'm consuming this ArrayList from an ObjectDataSource and would like to have this support...
48
by: Alex Chudnovsky | last post by:
I have come across with what appears to be a significant performance bug in ..NET 2.0 ArrayList.Sort method when compared with Array.Sort on the same data. Same data on the same CPU gets sorted a...
4
by: M Harvey | last post by:
I have an arraylist that contains datetime values. What is the best way to sort this arraylist by date ascending? Thanks, Matt
3
by: jtfaulk | last post by:
Re: ArrayList, Sort, Menu, IComparer, Object, multidemensional I have a multi-dimensional arraylist, and I would like to sort one level of it but not all. The multi-dimensional arraylist...
8
by: SimeonArgus | last post by:
I need to sort a list of points, so I've considered using an IComparable implementation. Should be easy, right? But I need to know two things in the CompareTo function, not one. Test1: I need to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.