what is the difference between comparable and comparator interface

what is the difference between comparable and comparator interface
LVL 9
Manikandan ThiagarajanSenior consultantAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

for_yanCommented:
Comparable need to define method:

int       compareTo(T o)

Comparator defines methods
int       compare(T o1, T o2)

and

 boolean       equals(Object obj)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Manikandan ThiagarajanSenior consultantAuthor Commented:
what is the difference
0
for_yanCommented:
In order to sort Collection<E> its elemenst need to implemnet Comparable

public static <T extends Comparable<? super T>> void sort(List<T> list)

Bt you can aslo sort your own custom objects if you use
another method whaich takes type implementing Comaprator as arguments:

public static <T> void sort(List<T> list,
                            Comparator<? super T> c)
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

for_yanCommented:
Loook above they implemnent methods with different signatures
0
for_yanCommented:
Comparable takes one parameter and Comaparator takes two parameters
0
for_yanCommented:

14 down vote accepted
      

The text below comes from http://lkamal.blogspot.com/2008/07/java-sorting-comparator-vs-comparable.html

Comparable

A comparable object is capable of comparing itself with another object. The class itself must implements the java.lang.Comparable interface in order to be able to compare its instances.

Comparator

A comparator object is capable of comparing two different objects. The class is not comparing its instances, but some other class’s instances. This comparator class must implement the java.util.Comparator interface.
0
for_yanCommented:

This is also nice explanation:

Comparable lets a class implement its own comparison:

    it's in the same class (it is often an advantage)
    there can be only one implementation (so you can't use that if you want two different cases)

By comparison, Comparator is an external comparison:

    it is typically in a unique instance (either in the same class or in another place)
    you name each implementation with the way you want to sort things
    you can provide comparators for classes that you do not control
    the implementation is usable even if the first object is null
0
for_yanCommented:
When you define your custom class and want to sort it you can make this class Comparable i.e. implement Comparable interface

Alternatively you may not touch the class, but rather define external class which implements Comparator and use this
external object as parameter of sorting methiods
0
Manikandan ThiagarajanSenior consultantAuthor Commented:
thankyou for the analysis
0
Manikandan ThiagarajanSenior consultantAuthor Commented:
I've requested that this question be closed as follows:

Accepted answer: 0 points for stmani2005's comment http:/Q_27384931.html#37010218

for the following reason:

helpful
0
for_yanCommented:
so why did you request to close this without allcatiing points?
0
for_yanCommented:
I believe this question was answered. Perhaps author just pressed the wrong button ?
0
for_yanCommented:

I'd suggest that the solution should be split between posting http:#36928865 which explains
formal difference beween Comparable and Comparator in terms of method signatures
and http:#36928886 which shows the difference between them in terms of most popular
use cases.
0
South ModModeratorCommented:
All,
 
Following an 'Objection' by for_yan (at http://www.experts-exchange.com/Q_27409984.html) to the intended closure of this question, it has been reviewed by at least one Moderator and is being closed as recommended by the Expert.
 
At this point I am going to re-start the auto-close procedure.
 
Thank you,
 
SouthMod
Community Support Moderator
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming Languages-Other

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.