PC Review


Reply
Thread Tools Rate Thread

How do I use CompareTo in my case

 
 
tony
Guest
Posts: n/a
 
      31st Aug 2006
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


 
Reply With Quote
 
 
 
 
Andy Bates
Guest
Posts: n/a
 
      31st Aug 2006
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

"tony" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
Andy Bates
Guest
Posts: n/a
 
      31st Aug 2006
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

"tony" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Confused by the CompareTo method of IComparable Author Microsoft C# .NET 3 10th Jul 2008 08:14 PM
problem with using CompareTo when having string Tony Microsoft C# .NET 1 28th May 2008 12:26 PM
Non-Transitive String.CompareTo Marc Gravell Microsoft Dot NET Framework 0 13th Nov 2006 01:15 PM
String.Compareto Strangeness Gerry Wheeler Microsoft Dot NET 2 8th Oct 2005 01:35 AM
CompareTo issue. =?Utf-8?B?QmVuIFNwaWdsZQ==?= Microsoft Dot NET 3 9th Aug 2004 07:37 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:48 PM.