How do I use CompareTo in my case

T

tony

Hello!

I have a class lets call it Test with four fields.
This Test class implement IComparable so CompareTo is therefore implemented
in this Test class.
One field in this Test class is an int called Order.
The other are of type double called EAF, C_min, C_aim and C_max

I implement this CompareTo but I have run into some problems.

I want this CompareTo to be implemented in two different ways for this Test
class.

First of all I want to sort a collection for Test objects on this Order
field.
Second I have another class where I implement Compare. To this Compare
method I pass two object which are ArrayList
and then from this Compare method calls a made to CompareTo to check if the
two arrayList are identical when looking at
fields EAF,C_main,C_aim and C_max.

The problem it that I can only have one CompareTo?

Have anybody any suggestion to how I can solve my problem?

//Tony
 
A

Andy Bates

Sounds like you have two requirements ordering and comparing.

1. To sort the collection (or a copy of) use the ArrayList.Sort method. You
can supply a custom IComparer to this to sort the collection how ever you
want (by Order in your example). You can create as many custom IComparer
objects are you require.

2. Use the IComparable to test for equality. It sounds like this solves your
second issue but the first was clouding the waters.

Information on the ArrayList.Sort method can be found here:

http://msdn2.microsoft.com/en-us/library/0e743hdt.aspx

HTH

- Andy
 
A

Andy Bates

Sounds like you have two requirements ordering and comparing.

1. To sort the collection (or a copy of) use the ArrayList.Sort method. You
can supply a custom IComparer to this to sort the collection how ever you
want (by Order in your example). You can create as many custom IComparer
objects are you require.

2. Use the IComparable to test for equality. It sounds like this solves your
second issue but the first was clouding the waters.

Information on the ArrayList.Sort method can be found here:

http://msdn2.microsoft.com/en-us/library/0e743hdt.aspx

HTH

- Andy
 

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

Top