By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,224 Members | 1,120 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,224 IT Pros & Developers. It's quick & easy.

IComparable vs IComparer ?

P: n/a
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.
Nov 15 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
-----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/oPDWwEwccQ4rWPgRAms1AJ9esPvldfXz4pvHKPpiNOiBrYopNA CfTnE3
zGByXZQQK28+E1QSoXM8lXE=
=ruXq
-----END PGP SIGNATURE-----

Nov 15 '05 #2

P: n/a
-----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/oPIGwEwccQ4rWPgRAgsyAKCAbfnWGoY7kZGpxbvVLaCn436VUg CdEBxr
x9YGirhTd8L8lZxCWEVbuF4=
=PGvj
-----END PGP SIGNATURE-----

Nov 15 '05 #3

P: n/a
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.
"Cybertof" <cy****************@gmx.net> wrote in message
news:MP************************@msnews.microsoft.c om...
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.

Nov 15 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.