aspose file tools*
The moose likes Java in General and the fly likes why there are two  interfaces in java  for sorting Purpose? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "why there are two  interfaces in java  for sorting Purpose?" Watch "why there are two  interfaces in java  for sorting Purpose?" New topic
Author

why there are two interfaces in java for sorting Purpose?

Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

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 )


Save India From Corruption - Anna Hazare.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

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

Joined: Apr 18, 2009
Posts: 2234

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

Joined: Jan 28, 2003
Posts: 4181
    
  21

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.


Steve
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

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

Joined: Oct 27, 2005
Posts: 19760
    
  20

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>.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4463
    
    8

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

Joined: Jan 28, 2003
Posts: 4181
    
  21

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

Joined: Jun 29, 2009
Posts: 100
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.


OCPJP 6-100%; Preparing for GATE11
Soumyajit Hazra
Ranch Hand

Joined: Jun 26, 2007
Posts: 136
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.


Java Programmer | SCJP 1.5 | SCWCD 1.4
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

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

Joined: Apr 18, 2009
Posts: 2234

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

Joined: Mar 11, 2005
Posts: 79

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


- Rohit
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why there are two interfaces in java for sorting Purpose?