• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Difference between Comparable and Comparator

 
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using comparable only one sort sequence can be created.

Using Comparator many sort sequences can be created.



In the above mentioned program am able to create multiple sort sequences using if block in compareTo ???
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

In the above mentioned program am able to create multiple sort sequences using if block in compareTo ???


No, the program doesn't create multiple sort sequences. In your program you are sorting on age if income is zero. What if I want to sort Farmer objects on the basis of age even when income is not zero. What if I want to sort the Farmer list on the basis of name (without age or income being zero). Sorting on different properties is possible through Comparator...
 
jose chiramal
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok thanks for the explanation. In the above program for the compare method also I have used similar logic. How can the compare method provide multiple sort sequences in the above program, based on the criteria you mentioned ?

 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't do this with the Comparable interface and the compareTo method. You'll need a Comparator for this. You can create a FarmerName comparator which will sort farmer objects on the basis of name, FarmerAge comparator will sort on basis of age etc. I'm giving you an example
 
jose chiramal
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Am sorry, am still not able to get this. The compare method in the comparator interface can return only one value at a time. How then are we saying that we can get multiple sort sequences using Comparator.

Am sorry the concept is still not clear to me.

Multiple sort sequences means we can sort using farmer name, age, income using a single compare method ??

Appreciate your help.
 
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We can have multiple sort sequences by implementing comparator interface. That means if we want to compare the Farmer on the bases of farmer name, age, income we can create three different class which will implement the comparator class and implement the required logic in each respective class. Like nameComparator class can have logic to compare the Farmer on the bases of name, incomeComparator class can have logic to compare the Farmer on the bases of income and ageComparator class can have logic to compare the Farmer on the bases of age. Hence we can have multiple compare sequences and we do not have to update the Farmer class. But in case of comparable interface whatever in the compareTo method, that will be hard coded means there will be single compare sequence and we have to update the Farmer class if we want to change the compare sequence.


Hope this will work.
 
jose chiramal
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok thanks for the explanation, similar to how we have two or three classes implementing the Comparator interfaces, can we not have more than one class implementing comparable interface and thereby have multiple sort sequences ?
 
Ranch Hand
Posts: 310
1
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jose chiramal wrote:Ok thanks for the explanation, similar to how we have two or three classes implementing the Comparator interfaces, can we not have more than one class implementing comparable interface and thereby have multiple sort sequences ?



If you have 3 classes, and all implements Comparable, you will get 3 classes with different sort sequences.




But using Comparator<T>, you can have 100 Sort sequences for the same class if you want! With Comparable<T> there will always be one sort sequence for one class!

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jose chiramal wrote:Ok thanks for the explanation, similar to how we have two or three classes implementing the Comparator interfaces, can we not have more than one class implementing comparable interface and thereby have multiple sort sequences ?



Implement Comparable for the "natural" sorting order your Class needs. Use Comparator for additional sorting sequence other than default sorting. For example, my Customer class may need sorting by customerId by default. I will implement LocationComparator and InvoiceAmountComparator to sort customers by other attributes.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic