• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is the difference between Comparator and Comparable

 
Hemant Agarwal
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the difference between Comparator and Comparable?
 
Renato Losio
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A detailed explanation (with examples) on
ONJava

Extract from the article:

Implementing the Comparable interface enables you to define one way to compare instances of your class. However, objects are sometimes comparable in many ways. For example, two Person objects may need to be compared by age or by last/first name. In cases like this, create a Comparator that defines how to compare two objects. To make objects comparable in two ways, then you need two comparators.
 
Sri Ram
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
U can get the details from here
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic