File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Comparator & Comparable

 
Ranganathan Kaliyur Mannar
Bartender
Posts: 1101
10
Java Netbeans IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 11833
181
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranganathan Kaliyur Mannar
Bartender
Posts: 1101
10
Java Netbeans IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1101
10
Java Netbeans IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My post wasn't moved then? and nobod has replied!
maybe I myself should move it to an appropriate forum?
 
in Mkumar
Greenhorn
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1101
10
Java Netbeans IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 11833
181
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1101
10
Java Netbeans IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic