Results 1 to 9 of 9
Thread: Doubt About CompareTo method() ?
- 03-07-2011, 11:48 AM #1
Doubt About CompareTo method() ?
Dear All,
This is my Man class . I have to implement the comparable interface in my Man class for sorting .I have to check both age and name in my compareTo method. For Ex if two man are same age , I have to sort depend on the name.Java Code:class Man implements Comparable<Man> { String name; int age; public Man(String name,int age) { this.age=age; this.name=name; } }
How can we do this ?
The preceding code is my try. I am not sure whether it is right or not.Java Code:public int compareTo(Man obj) { if(age > obj.age ) return 1; else if(age < obj.age ) return -1; else return name.compareTo(obj.name); }Mak
(Living @ Virtual World)
- 03-07-2011, 11:59 AM #2
Yeah You wrote a correct code. What are you not sure? You can find more information in API String#compareTo
Skype: petrarsentev
http://TrackStudio.com
- 03-07-2011, 12:40 PM #3
Thanks Dude.
What if I have to compare depend on age and grade!
Is this right code.
public int compareTo(Man obj) {
if(age > obj.age ) return 1;
else if(age < obj.age ) return -1;
else {
if(grade> obj.grade) return 1;
else if(grade< obj.grade) return -1;
else return 0;
}
}Mak
(Living @ Virtual World)
- 03-07-2011, 02:13 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,459
- Rep Power
- 16
Can't you write a simple test to see if it works?
Wouldn't that be quicker than posting here and waiting for an answer?
- 03-08-2011, 04:46 AM #5
Tolls,
I can , I did so. It s working fine. Is this a good way to implement comparable interface?Mak
(Living @ Virtual World)
- 03-08-2011, 05:12 AM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
For things like ints, consider replacing the two if statements with a subtraction.
- 03-08-2011, 05:24 AM #7
Thanks pbrockway2.
I think if we do subtraction for ints we could not get the rigt result, could't we ?
If possible can u please post ur sample code here?Mak
(Living @ Virtual World)
- 03-08-2011, 06:16 AM #8
Note that the contract of Comparable requires the return value to be positive, negative or zero -- not necessarily +1 / -1 / 0.Java Code:if(age == obj.age) { return grade - obj.grade; } else { return age - obj.age; }
db
- 03-08-2011, 06:19 AM #9
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
I think if we do subtraction for ints we could not get the rigt result, could't we ?
The best way to see if to try it by writing a simple by-age comparator for your Man class. (compareTo() being a one line method!). I think it's perfectly possible for such a comparator to sucessfully sort some same data by age. If you think not, post your code illustrating this.
Similar Threads
-
using the compareTo method
By anonymous445 in forum New To JavaReplies: 3Last Post: 01-19-2011, 03:53 AM -
need help wit this compareTo method
By ShinTec in forum New To JavaReplies: 4Last Post: 02-02-2010, 02:24 AM -
ArrayLists compareTo method, equals method
By random0munky in forum New To JavaReplies: 2Last Post: 10-26-2009, 07:20 PM -
[SOLVED] Sorting / compareTo method confusion. Please help
By kbullard516 in forum New To JavaReplies: 8Last Post: 03-19-2009, 09:38 PM -
how compareTo Method works
By nanaji in forum Advanced JavaReplies: 1Last Post: 06-22-2008, 07:40 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks