Results 1 to 9 of 9
  1. #1
    makpandian's Avatar
    makpandian is offline Senior Member
    Join Date
    Dec 2008
    Location
    Chennai
    Posts
    450
    Rep Power
    6

    Default Doubt About CompareTo method() ?

    Dear All,

    Java Code:
    class Man 
    implements Comparable<Man>
    {
    	
    	String name;
    	int age;
    	
    	public Man(String name,int age) {
    		
    		this.age=age;
    		
    		this.name=name;
    		
    	}
    }
    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.

    How can we do this ?

    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);
    		
    		
    	}
    The preceding code is my try. I am not sure whether it is right or not.
    Mak
    (Living @ Virtual World)

  2. #2
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    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

  3. #3
    makpandian's Avatar
    makpandian is offline Senior Member
    Join Date
    Dec 2008
    Location
    Chennai
    Posts
    450
    Rep Power
    6

    Default

    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)

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,183
    Rep Power
    20

    Default

    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?

  5. #5
    makpandian's Avatar
    makpandian is offline Senior Member
    Join Date
    Dec 2008
    Location
    Chennai
    Posts
    450
    Rep Power
    6

    Default

    Tolls,

    I can , I did so. It s working fine. Is this a good way to implement comparable interface?
    Mak
    (Living @ Virtual World)

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    For things like ints, consider replacing the two if statements with a subtraction.

  7. #7
    makpandian's Avatar
    makpandian is offline Senior Member
    Join Date
    Dec 2008
    Location
    Chennai
    Posts
    450
    Rep Power
    6

    Default

    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)

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default

    Java Code:
    if(age == obj.age) {
      return grade - obj.grade;
    } else {
      return age - obj.age;
    }
    Note that the contract of Comparable requires the return value to be positive, negative or zero -- not necessarily +1 / -1 / 0.

    db

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    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

  1. using the compareTo method
    By anonymous445 in forum New To Java
    Replies: 3
    Last Post: 01-19-2011, 04:53 AM
  2. need help wit this compareTo method
    By ShinTec in forum New To Java
    Replies: 4
    Last Post: 02-02-2010, 03:24 AM
  3. ArrayLists compareTo method, equals method
    By random0munky in forum New To Java
    Replies: 2
    Last Post: 10-26-2009, 08:20 PM
  4. Replies: 8
    Last Post: 03-19-2009, 10:38 PM
  5. how compareTo Method works
    By nanaji in forum Advanced Java
    Replies: 1
    Last Post: 06-22-2008, 08:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •