• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Collections.sort() : need guidance for my code

 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?


 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at the Comparable Interface. You need to override compareTo method-
 
Sunny Bhandari
Ranch Hand
Posts: 448
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also use Comparable interface. Depends upon the situation.
Comparable based solution will be faster.
 
Rob Spoor
Sheriff
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
done. here is the code and the output :

 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49823
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In which case you ought to have asked for more explanation at the time.
 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49823
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think there was a mistake about getting 0.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic