Results 1 to 3 of 3
  1. #1
    Albert is offline Senior Member
    Join Date
    Jun 2007
    Posts
    114
    Rep Power
    0

    Default Problem with sort method

    Does anyone know how I would go about writing a method that sorts using this set up?

    This is what I have so far, but it's complain that there's any error with the .sort.

    Java Code:
    public static void sort(ArrayList<Insurance> insure)
    {
    Insurance.sort(insure);
    }
    Thanks.

    Albert:rolleyes:

  2. #2
    Daniel is offline Member
    Join Date
    Jun 2007
    Posts
    91
    Rep Power
    0

    Default

    I think you want to use the Collections helper class. It has static methods to help you sort things like ArrayList's. However, you will need to make sure that the Insurance class implements Comparable which means that it has to have a compareTo() method. Take a look at the docs - it's really not too bad.

    Greetings.

    Daniel:o

  3. #3
    Albert is offline Senior Member
    Join Date
    Jun 2007
    Posts
    114
    Rep Power
    0

    Default

    I have this for my compareTo() and when I run the Collections.sort(insure) it just skips over the comand?

    Java Code:
    public int compareTo(Object obj)
    {
    Insurance insure = (Insurance) obj;
    if (insure.getCustomer().getCustID() < this.getCustomer
    ().getCustID())
    return 1;
    else if (insure.getCustomer().getCustID() > this.getCustomer
    ().getCustID())
    return -1;
    else
    return 0;
    }
    Thanks.

    Albert:rolleyes:

Similar Threads

  1. Replies: 2
    Last Post: 03-26-2010, 05:12 PM
  2. problem with split method
    By abhiN in forum New To Java
    Replies: 7
    Last Post: 02-10-2009, 01:54 PM
  3. 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
  4. sort
    By Camden in forum New To Java
    Replies: 7
    Last Post: 11-28-2007, 01:11 AM
  5. how to sort
    By Feng in forum New To Java
    Replies: 1
    Last Post: 11-20-2007, 06:56 AM

Posting Permissions

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