Connecting Tech Pros Worldwide Help | Site Map

IComparable vs IComparer ?

 
LinkBack Thread Tools Search this Thread
  #1  
Old November 15th, 2005, 01:25 PM
Cybertof
Guest
 
Posts: n/a
Default IComparable vs IComparer ?

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.



  #2  
Old November 15th, 2005, 01:25 PM
Ray Hsieh (Ray Djajadinata)
Guest
 
Posts: n/a
Default Re: IComparable vs IComparer ?

-----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-----

  #3  
Old November 15th, 2005, 01:25 PM
Ray Hsieh (Ray Djajadinata)
Guest
 
Posts: n/a
Default Re: IComparable vs IComparer ?

-----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-----

  #4  
Old November 15th, 2005, 01:25 PM
Fred Mellender
Guest
 
Posts: n/a
Default Re: IComparable vs IComparer ?

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" <cybertof2003nospam@gmx.net> wrote in message
news:MPG.1a0b011255c404f49896c1@msnews.microsoft.c om...[color=blue]
> 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.
>
>[/color]


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.