Results 1 to 5 of 5
  1. #1
    Tabula Rasa is offline Member
    Join Date
    Apr 2011
    Location
    USA
    Posts
    15
    Rep Power
    0

    Default compareTo method

    Assuming there are methods called getLast() and getFirst()

    Java Code:
    public int compareTo(Object n)
        {
           Student s = (Student)n;
           int num = lastname.compareTo(s.getLast());
           return (num != 0 ? num : firstname.compareTo(s.getFirst()));
        }
    I don't know how to interpret the method body. I know there is another way to write this code, but I am not sure what that is.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    [ignore]I don't think you should be using getLast or getFirst (did your teacher suggest this, or if not, what are you trying to achieve by using this?) but rather simply comparing the current object with n.[/ignore]

    Edit: Please ignore this dreck that I've posted above. -- I was not thinking but thought that you were getting the last or first items from a list not first and last names. :(
    Last edited by Fubarable; 04-21-2011 at 04:38 PM.

  3. #3
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    It compares two Student objects by their last names. If these are equal, it will compare their first names!

  4. #4
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    sort a List of students "studentList" by first name:

    Java Code:
    Collections.sort(studentList, new Comparator(){
    
                public int compare(Object o1, Object o2) {
                    Student s1 = (Student) o1;
                    Student s2 = (Student) o2;
                    int num = s1.getFirstName().compareToIgnoreCase(s2.getFirstName());
                    boolean equal = num == 0;
                    return equal? s1.getLastName().compareToIgnoreCase(s2.getLastName()) : num;
                }
     
            });
    compareToIgnoreCase returns:
    a negative integer, zero, or a positive integer as the the specified String is greater than, equal to, or less than this String, ignoring case considerations.
    Last edited by ozzyman; 04-21-2011 at 04:38 PM. Reason: added last name comparison

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    This original code, is it in a class that implements comparable? Have you tried getting a list of student objects and try sorting it? The original code you posted looks fairly decent, the easiest way to test it is to actually run it.

Similar Threads

  1. Doubt About CompareTo method() ?
    By makpandian in forum New To Java
    Replies: 8
    Last Post: 03-08-2011, 06:19 AM
  2. using the compareTo method
    By anonymous445 in forum New To Java
    Replies: 3
    Last Post: 01-19-2011, 03:53 AM
  3. need help wit this compareTo method
    By ShinTec in forum New To Java
    Replies: 4
    Last Post: 02-02-2010, 02:24 AM
  4. ArrayLists compareTo method, equals method
    By random0munky in forum New To Java
    Replies: 2
    Last Post: 10-26-2009, 07:20 PM
  5. how compareTo Method works
    By nanaji in forum Advanced Java
    Replies: 1
    Last Post: 06-22-2008, 07: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
  •