• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Use of comparator and comparable interface

 
Sanjib Pal
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15452
42
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Thanks everybody. Those replays cleared my concepts.

Sanjib
 
Venkata Krishn Rao Maddu
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks it helps to clear the concept of comparable and comparator
 
Campbell Ritchie
Sheriff
Pie
Posts: 49849
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

You will find more details here, here and here, as well as old threads on this forum.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic