473,385 Members | 1,465 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,385 software developers and data experts.

How do I use CompareTo in my case

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
Aug 31 '06 #1
2 9558
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" <jo*****************@telia.comwrote in message
news:Ob**************@TK2MSFTNGP02.phx.gbl...
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


Aug 31 '06 #2
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" <jo*****************@telia.comwrote in message
news:Ob**************@TK2MSFTNGP02.phx.gbl...
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


Aug 31 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ben Spigle | last post by:
I have a situtation where CompareTo is returning strange results when having dashes in strings. The most concise illustration is: "10050".CompareTo("1005-") = 1 "10050".CompareTo("1005-0") = -1...
5
by: majm | last post by:
I'm trying to implement strongly typed lists in the 2.0 framework. I'm using VS2005 beta 2. So far, System.Collections.Generic.List appears to be the ideal solution. However, the...
3
by: Charlie Bear | last post by:
i wonder if anyone can help... i have an abstract class called "Message" that impliment's icompareable on the property "TimeSent". i have two types of messages that inherit the "Message" class -...
3
by: Fir5tSight | last post by:
Hi All, I have an interface class defined as follows: class FileName : IComparable { public FileName(string fileName, string packageName) { // // TODO: Add constructor logic here
4
by: BenCoo | last post by:
Hello, In a Binary Search Tree I get the error : Object must be of type String if I run the form only with the "Dim bstLidnummer As New BinarySearchTree" it works fine. Thanks for any...
4
authorityaction
by: authorityaction | last post by:
I am writing a polynomial class that later I will use to add two polynomials together. Right now I am working on writing the compareTo method (my class implements Comparable). Unfortunately I am...
2
by: Tony | last post by:
Hello! I have method CompareTo written below. But I have a problem because I want to sort on HeatNumber which is of type int and if this is the same on MspName which is a string. But according...
2
by: Jon Skeet [C# MVP] | last post by:
Author <gnewsgroup@gmail.comwrote: Where? It looks to me like it's calling double's implementation of CompareTo. -- Jon Skeet - <skeet@pobox.com> Web site: http://www.pobox.com/~skeet Blog:...
3
by: raylopez99 | last post by:
This is an example of using multiple comparison criteria for IComparer/ Compare/CompareTo for List<and Array. Adapted from David Hayden's tutorial found on the net, but he used ArrayList so the...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.