• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

Comparable vs Comparator

 
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Comparable vs Comparator

1) It is necessary to modify the class whose instance is going to be sorted. 1) A separate class can be created in order to sort the instances.

2)Only one sort sequence can be created. 2) Many sort sequence can be created.




Q1) I don't understand what these two points 1&2 mean to say in Comparator ?


Example:
Suppose Employee class which implements Comparable interface.It is sorted using empid.To sort it age wise.I have to modify this class so that it can be sorted age wise.

Q1) Does this point 1 means : You have to modify same class again to make it sorted as age wise.

Q2) Please refer Q2 below


Comparable



Comparator

Q1)A separate class can be created in order to sort the instances?
does this point means that each time you have to create different classes for different sorting requirements.



Q2) Many sort sequence can be created?
We are creating different classes for different sorting requirements. so how we can say we can create many sort sequences?What is use of adding new classes each time
as per requirement changes?
so if this is a case, we can create many sort sequences in Comparable as well.We can go on adding new classes each time as per requirement changes?


 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

amolpalekar kadolkar wrote:
Comparable : It is necessary to modify the class whose instance is going to be sorted.
Comparator :1) A separate class can be created in order to sort the instances.



look at the example : http://lkamal.blogspot.com/2008/07/java-sorting-comparator-vs-comparable.html

<edit>I use Comparator rather than Comparable, since you can separate *compare logic* from the class(which instance needs to sort) to other class</edit>
 
He's giving us the slip! Quick! Grab this tiny ad!
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic