Difference between Comparator.compare() and Comparable.compareTo()

Plz tell me the difference(s) between Comparator.compare() and Comparable.compareTo()
[92 byte] By [biswa_orkuta] at [2007-11-27 6:27:52]
# 1
Did you read the API docs? Do you know what Comparator and Comparable are used for?
CeciNEstPasUnProgrammeura at 2007-7-12 17:50:01 > top of Java-index,Java Essentials,Java Programming...
# 2
> Plz tell me the difference(s) between> Comparator.compare() and Comparable.compareTo()[url= http://java.sun.com/docs/books/tutorial/collections/interfaces/order.html]The Java?Tutorial - Object Ordering[/url]~
yawmarka at 2007-7-12 17:50:01 > top of Java-index,Java Essentials,Java Programming...
# 3
The names, the owning types and the signatures
georgemca at 2007-7-12 17:50:01 > top of Java-index,Java Essentials,Java Programming...
# 4
>Did you read the API docs? >Do you know what Comparator and Comparable are used for? They are used for sorting. Collections.sort() requires the class whose objects have to be sorted to implement any of these interfaces.
biswa_orkuta at 2007-7-12 17:50:01 > top of Java-index,Java Essentials,Java Programming...
# 5

> >Did you read the API docs?

> >Do you know what Comparator and Comparable are used

> for?

>

> They are used for sorting. Collections.sort()

> requires the class whose objects have to be sorted to

> implement any of these interfaces.

There was an armed robbery last week, and the thieves used a car to escape. So can we safely define "car" as "a mode of transport employed by armed robbers"?

georgemca at 2007-7-12 17:50:01 > top of Java-index,Java Essentials,Java Programming...
# 6
> requires the class whose objects have to be sorted to> implement any of these interfaces.What, any? It requires the class to implement Comparable. Basta.
quittea at 2007-7-12 17:50:01 > top of Java-index,Java Essentials,Java Programming...
# 7

>Did you read the API docs?

> >Do you know what Comparator and Comparable are used

> for?

>

> They are used for sorting. Collections.sort()

> requires the class whose objects have to be sorted to

> implement any of these interfaces.

>There was an armed robbery last week, and the thieves used a car to escape.

>So can we safely define "car" as "a mode of transport employed by armed robbers"?

No, definitely cant....i meant that's the case what i came accross of them...i would like to know how do they differ funtionally...

biswa_orkuta at 2007-7-12 17:50:01 > top of Java-index,Java Essentials,Java Programming...
# 8

> >Did you read the API docs?

> > >Do you know what Comparator and Comparable are

> used

> > for?

> >

> > They are used for sorting. Collections.sort()

> > requires the class whose objects have to be sorted

> to

> > implement any of these interfaces.

>

> >There was an armed robbery last week, and the

> thieves used a car to escape.

> >So can we safely define "car" as "a mode of

> transport employed by armed robbers"?

>

> No, definitely cant....i meant that's the case what i

> came accross of them...i would like to know how do

> they differ funtionally...

Don't their respective signatures give you any clue?

georgemca at 2007-7-12 17:50:01 > top of Java-index,Java Essentials,Java Programming...
# 9
> i would like to know how do they differ funtionally...Reply #2.~
yawmarka at 2007-7-12 17:50:01 > top of Java-index,Java Essentials,Java Programming...
# 10
> i would like to know how do they differ funtionally... >Reply #2.compare() has both the objects as arguments which are needed to be compared....so through which object i should call it....
biswa_orkuta at 2007-7-12 17:50:01 > top of Java-index,Java Essentials,Java Programming...
# 11

It's probably not put formally correct.

Comparable: something that can be compared -> sets the default (natural) way of comparing/ordering

Comparator: something that can compare -> can be used in case multiple different ways are required to compare 2 objects of some type. So when you call compare, you use an instance of the class that implements the comparator...

Peetzorea at 2007-7-12 17:50:01 > top of Java-index,Java Essentials,Java Programming...
# 12

> compare() has both the objects as arguments which are

> needed to be compared....so through which object i

> should call it....

The compare() method should be called on the instance of Comparator, and passed references to the two objects you wish to compare. Alternately, you can pass the Comparator to existing utility methods, whereby you won't be calling compare() directly; e.g., Collections.sort().

Go through the tutorial again. It answers your questions and give you examples.

~

yawmarka at 2007-7-12 17:50:01 > top of Java-index,Java Essentials,Java Programming...