This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Use of comparator and comparable interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Use of comparator and comparable interface" Watch "Use of comparator and comparable interface" New topic
Author

Use of comparator and comparable interface

Sanjib Pal
Greenhorn

Joined: Feb 08, 2010
Posts: 27
Hi All,

Could anybody please specify when we should use the comparable interface to sort the collection and when to use comparator interface? Since both of them are use for sort any collection. Please specify in details...

Thanks,
Sanjib


SCJP 5.0
Try unless you get the ultimate success.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13869
    
  10

To be able to use the Comparable interface, the class of the objects in the collection has to implement that interface. You might have for example a List of objects from a class that you don't control yourself - for example, a class that comes from some third-party library. You can't make that class implement Comparable easily (it would mean you'd have to change the source code of the third-party library, which is normally not a good idea).

To be able to sort the List anyway, you can implement your own Comparator and pass that to for example Collections.sort(list, comparator);. Your Comparator object can be separate from the objects that are in the List.

Another example is when you might want to sort the list in different ways. Suppose you have a Person class:

Suppose that you have a List of Person objects, and that you at one time want to sort the List by name, but at another time sort the List by age. You could make class Person implement Comparable, but you can implement the compareTo() method in only one way.

By using Comparator, you could instead make different comparators, one that compares Person objects by name, and another one that compares them by age. If you want to sort by name, you use the first Comparator object, and if you want to sort by age you use the other Comparator object.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

Comparable should be used for the natural ordering - an ordering that just seems logical. For numbers that numerical ordering, for Strings its lexicographical ordering, for a Person class it could be the last name first, first name second. Basically, an ordering you want by default.

Comparator should be used if you don't want a default natural ordering, you can't make your class Comparable (perhaps because you get it from a library) or you want a different way of ordering (e.g. on first name first, last name second for the Person class).


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Jesper Young wrote:
By using Comparator, you could instead make different comparators, one that compares Person objects by name, and another one that compares them by age. If you want to sort by name, you use the first Comparator object, and if you want to sort by age you use the other Comparator object.


by this way, you can move the compare *logic* to a seperate class , which looks good .
Sanjib Pal
Greenhorn

Joined: Feb 08, 2010
Posts: 27
Hi all,

Thanks everybody. Those replays cleared my concepts.

Sanjib
Venkata Krishn Rao Maddu
Greenhorn

Joined: Apr 02, 2008
Posts: 3
Comparable interface is having compareTo() method and which is normally used for natural ordering.
Comparator interface is having compare() method which takes two arguments and it can be used whenever you want to sort objects based of more then one parameter.

Example:

Comparable Interface:



Comparator Interface:

SoniyaG Gupta
Greenhorn

Joined: Aug 03, 2012
Posts: 1
Thanks it helps to clear the concept of comparable and comparator
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
Welcome to the Ranch

You will find more details here, here and here, as well as old threads on this forum.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Use of comparator and comparable interface
 
Similar Threads
Comparator & Comparable
TreeSet - Provider Sort Method
Soting objects in collection
Comparator and Compatable Query
doubt....