• 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
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Comparator vs comparable

 
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Q1)It is necessary to modify the class whose instance is going to be sorted? What does it means?

Q2) From below example please tell where this has bee done?





Q3) A seperate class can be created in order to sort the instance? what does it mean?
Q4) Where seperate class is created in this example?
Q5) How we can create many sort instances in this example?


 
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In that first sample class, the class was modified by making it implement the Comparable interface, and thus the compareTo method.

The example there doesn't show a second class implemented differently. but assuming instead of wanting to have many instances of this class sorted by the property sorted here, we could subclass it and override the compareTo method.

in this example, it doesn't really show why we would do that, consider a class that had more than one string property, maybe an id, an age, a name. So the compareTo in this person class might by default sort by name. Now if we created a subclass birthday list entry say, to have it sort by age instead.

so jamming the object instances into something that naturally sorts, such as a TreeSet, or using a sort operation on the colletion, would cause the instances of person class to be sorted differently from the instances of birthday list class. I guess we could have some configurable external proparty that each object instance would know how to retreive somehow so that we could configure sorting by different properties in the one class, where this variable would drive the compareTo method implemented here, that would not need to have a new object type subclassed etc to have different sorting.

in that second example, you seem to be wanting to compare something that implements a comparable interface with something that does not. in that case you would have to come up with your own code that meticulously inspects each property in the A, and B ones, so that you end up coming with something that returns -1, 0,or +1 in a manner like the built in compare thingie would, but in this case, unless you made this logic into a wrapper class that implements Comparable interface, the Java built int utilities for comparing (or sorting) would not be able to make use of this.
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The difference between the comparable and comparator is that

1)In comparable the particular class itself will compare it with another object.

2)In Comparator a third party will compare two objects and return the result..

It is better to use the comparable interface
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

PrasannaKumar Sathiyanantham wrote:It is better to use the comparable interface


Better than what ?
 
Satyajeet Kadam
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please correct me if i am wrong?

1)In comparable the particular class itself will compare it with another object.

Do you mean to say that we are making the 2 objects of same class and comparing them with one another.Can we say this statement is same as the following
statement "You must modify the class whose instance you want to sort" .

2) In Comparator a third party will compare two objects and return the result


Do you mean to say that we will create a seperate class that implements comparator and write sorting logic as per our requirement and call Collection.sort(list,comparator).










 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes
 
Get meta with me! What pursues us is our own obsessions! But not this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic