Results 1 to 8 of 8
  1. #1
    ScienceLife is offline Member
    Join Date
    Nov 2010
    Posts
    7
    Rep Power
    0

    Exclamation Sort with CompareTo

    hello!
    I really need the help of anyone that knows!

    I am having a problem with sorting an ArrayList.
    I have the following code that sorts the ArrayList by first name, but I want it to be able to sort by last name too:

    Java Code:
    public int compareTo(Object o){
            People temp = (People) o;
            return (firstname.compareTo(temp.firstname));
     }
    
    public void sortPeople()
        {
            Collections.sort(human); // sorts by the first name of people
            //And then I load them into a table
        }

  2. #2
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default

    I think that you can only return one value. So you will have to make a new method.
    Last edited by PhQ; 11-30-2010 at 10:37 PM.

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

    Default

    If you want to sort by last name and first name, then in the compareTo method, call compareTo on the last name Strings and if it returns 0, call it on the first name Strings and return that result. If you want to give the user the option of sorting by last name or by first name, then you'll need to create Comparator objects.

  4. #4
    ScienceLife is offline Member
    Join Date
    Nov 2010
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    If you want to sort by last name and first name, then in the compareTo method, call compareTo on the last name Strings and if it returns 0, call it on the first name Strings and return that result. If you want to give the user the option of sorting by last name or by first name, then you'll need to create Comparator objects.
    I want the user to have the option to sort by first name or last name!
    how can i do that with Comparator object, I tried but didn't manage much? :/

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

  6. #6
    ScienceLife is offline Member
    Join Date
    Nov 2010
    Posts
    7
    Rep Power
    0

    Default

    Well I had mainly thought of doing an if statement like:

    Java Code:
    public int compareTo(Object o){
            People temp = (People) o;
    
            if(sortPeople())
            {
                   return (firstname.compareTo(temp.firstname));
            }
            else // i mean if (sortPeople2())
            {
                   return (lastname.compareTo(temp.lastname));
             }
             
            
     }
    
    public void sortPeople()
        {
            Collections.sort(human); // sorts by the first name of people
            //And then I load them into a table
        }
    
    public void sortPeople2()
        {
            Collections.sort(human); // sorts by the first name of people
            //And then I load them into a table
        }
    but I don't seeim to manage this... I thought to do it with bubble sort but then I can't load it in my table :/

  7. #7
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default

    Java Code:
    public int compareTo(Object o, [B]boolean name[/B]){
            People temp = (People) o;
    
            if([B]name[/B] == true)
            {
                   return (firstname.compareTo(temp.firstname));
            }
            else // i mean if (sortPeople2())
            {
                   return (lastname.compareTo(temp.lastname));
             }
             
            
     }
    
    public void sortPeople()
        {
            Collections.sort(human); // sorts by the first name of people
            //And then I load them into a table
        }
    
    public void sortPeople2()
        {
            Collections.sort(human); // sorts by the first name of people
            //And then I load them into a table
        }
    is that what you want?

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    PhQ: That breaks the compareTo() contract.

    ScienceLife: Using what you have there at the moment you could change the if to use a flag. Something like "if (sortByLastname) {return compare last names} else {compare first names}"?

Similar Threads

  1. need help wit this compareTo method
    By ShinTec in forum New To Java
    Replies: 4
    Last Post: 02-02-2010, 02:24 AM
  2. Help with interfaces, compareTo etc
    By jigglywiggly in forum New To Java
    Replies: 10
    Last Post: 09-07-2009, 01:52 AM
  3. Using Merge Sort to sort an ArrayList of Strings
    By coldfire in forum New To Java
    Replies: 3
    Last Post: 03-13-2009, 01:03 AM
  4. compareTo()
    By Tsiliadoros in forum Advanced Java
    Replies: 5
    Last Post: 10-03-2008, 01:18 PM
  5. How to sort a list using Bubble sort algorithm
    By Java Tip in forum Algorithms
    Replies: 3
    Last Post: 04-29-2008, 08:04 PM

Tags for this Thread

Posting Permissions

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