How do I use CompareTo in my case

  • Thread starter Thread starter tony
  • Start date Start date
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
 
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
 
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
 
Back
Top