File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes OO, Patterns, UML and Refactoring and the fly likes Comparator & Comparable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Comparator & Comparable" Watch "Comparator & Comparable" New topic
Author

Comparator & Comparable

Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1075
    
  10

Hi,
I have a problem where I have to sort a set of Employees. The employee object has attributes like id, name, designation. Sorting can be on any of the said attribute. The default sort will be based on the id or name.

Now, I have made Employee implement java.lang.Comparable - in which I have provided the default sort. I have 2 java.lang.Comparators for the other 2 sorts. I am trying to have the strategy pattern and decide the sorting. So, for convenience purpose, I write a Comparator for the default sorting too - inside this Comparator's compareTo - I call the emp1.compare(emp2)...

Is this ok? i.e does it make sense to write the comparison directly in the Comparator and not waste one more call?

I know there are different constructors in TreeSet for this scenario, but then I would need a 'if' in my code - which is what I am trying to avoid through the Strategy Pattern.


Ranga.
SCJP 1.4, OCMJEA/SCEA 5.0.
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11278
    
  59

I'm a little confused here.

If you are writing concrete code for your SCEA assignment, then you are doing far too much work.

If this is not related to your SCEA assignment, then it does not belong in this forum - let me know and I will move it to another forum.


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1075
    
  10

Its a query related to patterns - so, I thought this belongs here...also, I got this doubt when I was preparing for my SCEA...
however, if you think it doesn't belong here, feel free to move it...
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1075
    
  10

My post wasn't moved then? and nobod has replied!
maybe I myself should move it to an appropriate forum?
in Mkumar
Greenhorn

Joined: Sep 08, 2008
Posts: 20
Please check this link - http://lkamal.blogspot.com/2008/07/java-sorting-comparator-vs-comparable.html. This may will help you out.

-Manoj
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1075
    
  10

Manoj,
Thanks for the links. But I do know both these. My question is, can I call emp.compareTo within inside a Comparable - basically aimed at code reuse. I think I can though the need for it maybe wierd....
Dirk Weise
Greenhorn

Joined: Jun 19, 2010
Posts: 1
I suppose you have multiple collation sequences on employees, where one is the default. There is no reason why you shouldn't have a comparator delegating to compareTo(), though personally I'd rather reduce it to just comparators for clarity's sake - usually hard to tell what the 'default' is.

Andrew is right in both his comments, this doesn't really belong here.
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11278
    
  59

I'm moving this to the OO, Patterns, UML and Refactoring forum. If you are reading this, you are already there.
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1075
    
  10

Thanks Dirk Weise.

Its like, in the screen where I list employees (with paging), I provide the sorting option on various columns...
in screens where an employee has to be selected in a dropdown box - the list of employees have to be shown sorted by 'default' name - in this case, the object being Comparable helps...
and in the other screen, I would use Comparator for sorting - in this case, delegating is code reuse - saves me a few keystrokes!
 
jQuery in Action, 2nd edition
 
subject: Comparator & Comparable
 
Similar Threads
sorting table columns
Sorting ArrayList with Comparitor Interface for String property
vector sorting
object sorting in a Vector
Sorting arraylist on the basis of object