| Author |
Collections.sort() : need guidance for my code
|
Rahul Sudip Bose
Ranch Hand
Joined: Jan 21, 2011
Posts: 637
|
|
I have the following class which i want to sort based on String name or int year (of release) for movie DVD's :
How do i make the compare method for a comparator for this class ?
|
SCJP 6. Learning more now.
|
 |
Mohamed Sanaulla
Bartender
Joined: Sep 08, 2007
Posts: 2928
|
|
Have a look at the Comparable Interface. You need to override compareTo method-
|
Mohamed Sanaulla | My Blog
|
 |
Sunny Bhandari
Ranch Hand
Joined: Dec 06, 2010
Posts: 446
|
|
You can also use Comparable interface. Depends upon the situation.
Comparable based solution will be faster.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
|
Sunny, if you meant Comparator, then the statement about speed is nonsense. If there is any speed difference between using Comparable and using Comparator it is negligible.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Rahul Sudip Bose
Ranch Hand
Joined: Jan 21, 2011
Posts: 637
|
|
Rob Spoor wrote:Sunny, if you meant Comparator, then the statement about speed is nonsense. If there is any speed difference between using Comparable and using Comparator it is negligible.
speed...how do we measure that in practice ?
|
 |
Rahul Sudip Bose
Ranch Hand
Joined: Jan 21, 2011
Posts: 637
|
|
Using the below code i can sort objects based on name, which is a string. How to modify my code if it needs to be sorted on the basis of year, an int ?
Please help.
|
 |
Matthew Brown
Bartender
Joined: Apr 06, 2010
Posts: 3794
|
|
Rahul Sudip Bose wrote:Using the below code i can sort objects based on name, which is a string. How to modify my code if it needs to be sorted on the basis of year, an int ?
|
 |
Rahul Sudip Bose
Ranch Hand
Joined: Jan 21, 2011
Posts: 637
|
|
Matthew Brown wrote:
Rahul Sudip Bose wrote:Using the below code i can sort objects based on name, which is a string. How to modify my code if it needs to be sorted on the basis of year, an int ?
Thanks. I want to be able to choose whether want i want to Sort on the basis of string and an int not either. compare() can return only once , either the comparison of strings (my) code or ints(above). It want this code to be able to choose whether it should sort on the basis of int or string.
pseudocode :
Please advise.
rb
|
 |
Matthew Brown
Bartender
Joined: Apr 06, 2010
Posts: 3794
|
|
Rahul Sudip Bose wrote:Thanks. I want to be able to choose whether want i want to Sort on the basis of string OR an int and not either. compare can return only once , either the comparison of strings (my) code or ints(above). It does not choose the basis for sorting.
You do that by choosing which Comparator to use. Create classes for them both, and instantiate the one you need.
|
 |
Rahul Sudip Bose
Ranch Hand
Joined: Jan 21, 2011
Posts: 637
|
|
Matthew Brown wrote:
Rahul Sudip Bose wrote:Thanks. I want to be able to choose whether want i want to Sort on the basis of string OR an int and not either. compare can return only once , either the comparison of strings (my) code or ints(above). It does not choose the basis for sorting.
You do that by choosing which Comparator to use. Create classes for them both, and instantiate the one you need.
ta da... DOH !!! why didnt i think the obvious ! thanks a million !
|
 |
Rahul Sudip Bose
Ranch Hand
Joined: Jan 21, 2011
Posts: 637
|
|
done. here is the code and the output :
|
 |
Matthew Brown
Bartender
Joined: Apr 06, 2010
Posts: 3794
|
|
Well done! One suggestion (it doesn't change how it works, but it tidies things up a bit): you might consider making the Comparators (static) nested classes of DVD. That way you can refer to them as DVD.NameSort and DVD.YearSort, which ties them closely together with the DVD class (which makes sense since they're useless without it). As it is, it isn't obvious from the name (if the application gets bigger) that YearSort sorts only DVDs.
|
 |
Rahul Sudip Bose
Ranch Hand
Joined: Jan 21, 2011
Posts: 637
|
|
Matthew Brown wrote:Well done! One suggestion (it doesn't change how it works, but it tidies things up a bit): you might consider making the Comparators (static) nested classes of DVD. That way you can refer to them as DVD.NameSort and DVD.YearSort, which ties them closely together with the DVD class (which makes sense since they're useless without it). As it is, it isn't obvious from the name (if the application gets bigger) that YearSort sorts only DVDs.
Long ago i had a post "what are the uses of nested classes" and i did not get a simple answer which would not cover topics outside my knowledge. So can this be a good example of why we need nested classes ? Or is there a simpler example to show the need for inner classes ?
thanks
rb
PS : here is that old post
http://www.coderanch.com/t/528900/java/java/Practical-real-world-situations-where#2398461
|
 |
Mohamed Sanaulla
Bartender
Joined: Sep 08, 2007
Posts: 2928
|
|
Rahul Sudip Bose wrote:
Long ago i had a post "what are the uses of nested classes" and i did not get a simple answer which would not cover topics outside my knowledge. So can this be a good example of why we need nested classes ? Or is there a simpler example to show the need for inner classes ?
...
I did provide you an example
|
 |
Rahul Sudip Bose
Ranch Hand
Joined: Jan 21, 2011
Posts: 637
|
|
Mohamed Sanaulla wrote:
Rahul Sudip Bose wrote:
Long ago i had a post "what are the uses of nested classes" and i did not get a simple answer which would not cover topics outside my knowledge. So can this be a good example of why we need nested classes ? Or is there a simpler example to show the need for inner classes ?
...
I did provide you an example
Yes, but unfortunately i have not done GUI yet. So, i could not understand the meaning. Thanks for helping me.
regards
rb
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32704
|
|
|
In which case you ought to have asked for more explanation at the time.
|
 |
Tom Reilly
Rancher
Joined: Jun 01, 2010
Posts: 618
|
|
This code will not stand the test of time :-) For the following code,
What happens, for example, if one.year is Integer.MIN_VALUE and two.year is 1? Your code will claim them to be equal (subtracting one from Integer.MIN_VALUE results in zero). I point this out not for this example but if you have a situation where you are comparing ints that could be negative, you could have a bug.
|
 |
Rahul Sudip Bose
Ranch Hand
Joined: Jan 21, 2011
Posts: 637
|
|
Campbell Ritchie wrote:In which case you ought to have asked for more explanation at the time.
Yes. At that time i thought that i will study a bit of swing to understand the examples put before me. The k&b-scjp book also uses a theoretical GUI chat-client example which i did not understand. So, i decided to wait till i studied swing or if someone gave a newbie example.
|
 |
Rahul Sudip Bose
Ranch Hand
Joined: Jan 21, 2011
Posts: 637
|
|
Tom Reilly wrote:This code will not stand the test of time :-) For the following code,
What happens, for example, if one.year is Integer.MIN_VALUE and two.year is 1? Your code will claim them to be equal (subtracting one from Integer.MIN_VALUE results in zero). I point this out not for this example but if you have a situation where you are comparing ints that could be negative, you could have a bug.
I did not get 0 after doing what you said.
Code :
output :
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32704
|
|
|
I think there was a mistake about getting 0.
|
 |
 |
|
|
subject: Collections.sort() : need guidance for my code
|
|
|