IComparable vs IComparer ?

C

Cybertof

Hello,

Could someone please explain me the difference between the 2 interfaces
IComparable and IComparer ?

In which cases use one or the other ?
Are they dedicated to own classes or built-in collections ?


Regards,
Cybertof.
 
R

Ray Hsieh (Ray Djajadinata)

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Any class that implements IComparable is *comparable*. That is, you can
compare an instance of that class with another instance in a manner that
you define.

OTOH, a class that implements IComparer is a... well, a comparer (Java
people call this a Comparator), i.e.: a class that imposes ordering on a
set of objects.

When you want to sort an unsorted array of objects of the same class,
you can either make the class implement IComparable, or you can
implement IComparer in a separate class, and pass that to Array.Sort().
Of course if the comparison is done based on a *private* member of the
class, IComparable is more appropriate, since having your IComparator
implementation be able to access its private members may not be
possible/appropriate.

Cybertof wrote:

| Hello,
|
| Could someone please explain me the difference between the 2 interfaces
| IComparable and IComparer ?
|
| In which cases use one or the other ?
| Are they dedicated to own classes or built-in collections ?
|
|
| Regards,
| Cybertof.

- --
Ray Hsieh (Ray Djajadinata) [SCJP, SCWCD]
ray underscore usenet at yahoo dot com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (MingW32)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQE/oPDWwEwccQ4rWPgRAms1AJ9esPvldfXz4pvHKPpiNOiBrYopNACfTnE3
zGByXZQQK28+E1QSoXM8lXE=
=ruXq
-----END PGP SIGNATURE-----
 
R

Ray Hsieh (Ray Djajadinata)

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

private *and* not (supposed to be) exposed in any way to other classes,
I mean :)

Ray Hsieh (Ray Djajadinata) wrote:

| Of course if the comparison is done based on a *private* member of the
| class, IComparable is more appropriate, since having your IComparator
| implementation be able to access its private members may not be
| possible/appropriate.

- --
Ray Hsieh (Ray Djajadinata) [SCJP, SCWCD]
ray underscore usenet at yahoo dot com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (MingW32)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQE/oPIGwEwccQ4rWPgRAgsyAKCAbfnWGoY7kZGpxbvVLaCn436VUgCdEBxr
x9YGirhTd8L8lZxCWEVbuF4=
=PGvj
-----END PGP SIGNATURE-----
 
F

Fred Mellender

They have a variety of uses, but perhaps the most common is in sorting an
ArrayList.

Suppose you have a class Dog, and an ArrayList, List, of Dogs. You wish to
sort List by ageOfDog. You would have Dog implement the interface
IComparable. The implementation would cast the parameter in the CompareTo
method to a Dog, and return a value based on how this.ageOfDog compared to
((Dog)(obj)).ageOfDog). Then to sort the List, you could just invoke
List.Sort().

But suppose in another part of the application you would like to sort the
same List by nameOfDog. You have already "used up" the IComparable
interface that Sort uses. So, you can either set up a new class that
inherits from IComparer, or (if this is the only additional sort you need),
have Dog implement the IComparer interface too.
Then in the Compare method, which has two parameters, you would cast each to
a Dog and return a value based upon how the nameOfDog compares. Then to
invoke a sort by nameOfDog you would just say List.Sort(this) (or
List.Sort(someObjectThatImplementsIComparer)).

So: use IComparable when you want one of the objects in the comparison to
handle the compare. Use IComparer when this is not feasible or desirable,
and either you need to let a third object handle the compare, or want to do
different compare logic than has been already been used by IComparable.

Sort is not the only method that uses these interfaces (e.g. BinarySearch).
You may wish to invoke them yourself if you write a general routine that
needs to compare objects but wish to leave the details of the comparison up
to another object.
 

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