• 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

why there are two interfaces in java for sorting Purpose?

 
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,



Why did java Provided , Comparable and Comparator interfaces for sorting purposes ??

Isn't only Comparator isn't sufficient for comparing depending upon the Bean property ??

Why they have implemented so many things , (making hard for me to remember )
 
Saloon Keeper
Posts: 15705
367
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I like how you can give a class a 'natural order' by making it a Comparable. This is the interface I use most. In this case a class has an inherent order.

However, what happens when you have a class that is already comparable, but you want to sort it in a different way than its natural order? Then you can still provide a separate Comparator object.
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Stephan ,
that was a valuable information .
You mean to say that we must use Comparable interface to sort in a natural order only ??

Thanks in advnace .
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ravi Kiran V wrote:Thanks Stephan ,
that was a valuable information .
You mean to say that we must use Comparable interface to sort in a natural order only ??

Thanks in advnace .



What he means is that the Comparable interface defines the natural order, and Comparator provides a means of sorting objects in a different way than the natural order, or for sorting objects that do not have a natural order (do not implement the Comparable interface).

For example, books. Perhaps the 'Natural Order' for ordering books is through the Dewey Decimal System. If you were defining a book you would make it implements Comparable and the compareTo method would use the two books' Dewey Decimal index for sorting. But let's say later you want to sort by title, or by Author's last name... that is the job of a Comparator.
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Steve for the explanation .

What he means is that the Comparable interface defines the natural order



But when we implement the Comparable interface and override the compareTo method , cant we redefine the sorting order based on the return type of compareTo method ??




please advice , thanks in advance .
 
Sheriff
Posts: 22796
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's what Comparable is for. But what if you want to sort in two ways?
Suppose I make it implement Comparable<Employee> based on the name. But now I want to sort all my employees based on how long they've been employed. That's what Comparator is for.

Comparator is also for classes you can't change but don't implement Comparable, like java.awt.Color. I can't change it, but if I want to sort them (on RGB values) I can create a Comparator<Color>.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ravi Kiran V wrote:


Don't do that! That would mean all objects simultaneously come before and after all other objects. The result of sorting will depend on your sort alogorithm.
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ravi Kiran V wrote:But when we implement the Comparable interface and override the compareTo method , cant we redefine the sorting order based on the return type of compareTo method ??



Yes, and whatever you define in the Comparable's compareTo becomes the Object's natural order. But if you wanted to sort different ways you would need multiple implementations. If you only had the Comparable interface, then you would need multiple classes, each with a different definition of the compareTo method:

Then you have to be able to convert between them when you want to sort the same books different ways etc... it won't be easy.

Instead, you want just one class:


And have different ways of sorting them (Comparators):


Then you have a much simpler structure - a single book class which handles the 'natural' order, and utility Comparators which handle other sorting routines. No need to change Book or subclass it or anything...
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes you can define a new sort order.

Java has provided Comparator for sorting in more than one way.

Suppose you have a class DVDInfo implementing Comparable.
Now you have to override compareTo() method, and in that you define a particular sort order.

What will you do, if you want to define a different sort order now?
-Here comes the use of Comparator, you can simply make a new comparator class and pass it to the sort() method.


Comparable-lets you define only one sort order.
Comparator-lets you define more than one sort orders.
 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Same thing in a different way we can look suppose in a given scenario you have a Employee class but you don't have the source code or may be you don't have the permission to change any code but still you may need to sort it say by id or age or any other property. At this point using comparator is always useful keeping the code intact. That is the new code will not affect the existing one.
 
Stephan van Hulst
Saloon Keeper
Posts: 15705
367
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's more a matter of concept than practical results. In practice, you *can* just use Comparator to provide different sorting orders for the same class. However, some classes in concept have a natural, or default, order.

For instance, say you want to print the cast of a movie. Let's just say that actors have a natural or default order, namely alphabetical by name. You can implement the compareTo() method of an actor in such a way that the cast will be sorted this way.

However, some movies sort the actors by order of appearance. You can now provide a separate Comparator to do this, and still keep the default functionality. As a matter of fact, you would probably need a Comparator to do this, because an Actor can't decide where in the movie it appears, unless the Actor keeps a reference to the movie. A movie on the other hand, could provide a Comparator that sorts its actors in this fashion. Here is the full example:
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey thank you all of them , i think a learned a lot on Comparable and Comparator .

The points you made were very helpful .

With this site i am learning a lot which my Java Instructors didn't know exactly all these things .

Thanks once again.
 
Ranch Hand
Posts: 79
Eclipse IDE Spring Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:I like how you can give a class a 'natural order' by making it a Comparable. This is the interface I use most. In this case a class has an inherent order.

However, what happens when you have a class that is already comparable, but you want to sort it in a different way than its natural order? Then you can still provide a separate Comparator object.



Very right said Stephan & others - above lines are much clarifying than others i found on web .. or at least for me
 
A sonic boom would certainly ruin a giant souffle. But this tiny ad would protect it:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic