File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why there are two interfaces in java for sorting Purpose?

 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • 0
  • Mark post as helpful
  • send pies
  • 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 )
 
Stephan van Hulst
Bartender
Pie
Posts: 4826
34
Chrome Netbeans IDE Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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 .
 
Steve Luke
Bartender
Pie
Posts: 4181
21
IntelliJ IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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 .
 
Rob Spoor
Sheriff
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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>.
 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • 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
Pie
Posts: 4181
21
IntelliJ IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • 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...
 
Gari Jain
Ranch Hand
Posts: 100
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Soumyajit Hazra
Ranch Hand
Posts: 136
  • 0
  • Mark post as helpful
  • send pies
  • 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
Bartender
Pie
Posts: 4826
34
Chrome Netbeans IDE Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Rohit Mehta
Ranch Hand
Posts: 79
Chrome Eclipse IDE Spring
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic