0

I am trying to sort an ArrayList full of customer objects.

ArrayList Cust = new ArrayList();
Cust.add(new Customer("John",15,"New York",200));
Cust.add(new Customer("Moya",25,"Randfontein",200));                        
Cust.add(new Customer("Sue",44,"Jersey",100));
Cust.add(new Customer("Mpho",23,"London",250));

I have a foreign class, Sort that contains an insertion sort method that implements the Comparable interface.

public static <AnyType extends Comparable<? super AnyType>> void insertionSort(ArrayList<AnyType> a) {
        for (int p = 1; p < a.size(); p++) {
            AnyType tmp = a.get(p);
            int j;

            for (j = p; j > 0 && tmp.compareTo(a.get(j - 1)) < 0; j--) {
                a.set(j, a.get(j - 1));
            }
            a.set(j, tmp);
        }

I tried using this method in the main class to sort the ArrayList based on the Customer's age(second element).

Sorting.insertionSort(CustList);

This gives me an error though: no suitable method found for insertionSort(java.util.ArrayList) Sorting.insertionSort(CustList);

Help me ¯_(ツ)_/¯

as requested, Here is the full code :

   public interface Comparator<AnyType> {
        /**
         * Return the result of comparing lhs and rhs.
         * @param lhs first object.
         * @param rhs second object.
         * @return < 0 if lhs is less than rhs,
         *           0 if lhs is equal to rhs,
         *         > 0 if lhs is greater than rhs.
         */
        int compare( AnyType lhs, AnyType rhs );
    }
///

    public final class Customer implements Comparable<Customer> {

        String name;
        int age;
        String city;
        int loyaltyPoints;

        public Customer(String n, int a, String c, int lP) {
            name=n; age=a; city=c; loyaltyPoints=lP;
        }

        public String toString() {
            return "Customer " + name + " from " + city + " is " + age + " years old, and has loyaltypoints of " + loyaltyPoints;
        }

        public int compareTo(Customer rhs) {
            return this.age - rhs.age;
        }
    }
///
    public final class Sorting {


        public static <AnyType> void insertionSort(ArrayList<AnyType> a, Comparator<? super AnyType> cmp) {

        }



        public static <AnyType extends Comparable<? super AnyType>> void insertionSort(ArrayList<AnyType> a) {
            for (int p = 1; p < a.size(); p++) {
                AnyType tmp = a.get(p);
                int j;

                for (j = p; j > 0 && tmp.compareTo(a.get(j - 1)) < 0; j--) {
                    a.set(j, a.get(j - 1));
                }
                a.set(j, tmp);
            }
        }
    }

///
public class Test{

    public static void main(String[] args) {

        // Add some customers to an arraylist
        ArrayList Cust = new ArrayList();
        Cust.add(new Customer("John",45,"Tokyo",200));
        Cust.add(new Customer("Johna",25,"London",200));                        
        Cust.add(new Customer("James",33,"New York",100));
        Cust.add(new Customer("Jack",23,"Utah",250));                           
        Cust.add(new Customer("Janet",25,"Jersey",250));    
        Cust.add(new Customer("Jared",28,"Gotham",250));

        Sorting.insertionSort(Cust);

    }
}
1
  • Which java version are you on? Because the code you provided is working for 1.8? Although there can be many improvements, but as far as sorting is concerned its working. Commented Sep 10, 2017 at 8:26

1 Answer 1

2

Use ArrayList<Customer> custList = new ArrayList<>(); and make sure 'Customer' implements 'Comparable'.

The problem with ArrayList Cust is that Java can't assume anything about the type of the list elements except that they are Object, which is not Comparable.

Update:

  1. Your code as posted should compile and run, but Cust.add(...) should raise a warning 'Unchecked call to 'add(E)' as a member of raw type ArrayList'
  2. To fix the warning, replace the line ArrayList Cust = new ArrayList<>(); by ArrayList<Customer> Cust = new ArrayList<>();

Some other notes:

  1. Normally, your methods should expect interfaces instead of concrete classes, so you better replace the method signature public static <AnyType extends Comparable<? super AnyType>> void insertionSort(ArrayList<AnyType> a) with public static <AnyType extends Comparable<AnyType>> void insertionSort(List<AnyType> a) - note it expects 'List' instead of 'ArrayList'
  2. Variable names in java start with lower case, i.e. 'cust' instead of 'Cust'.
Sign up to request clarification or add additional context in comments.

4 Comments

It would add more value for other users if you explain "why" the OPs code doesn't work rather than a quick comment on how to fix it. Also, I am not on a computer to test this out but ArrayList<Customer> is not mandatory? ArrayList should be just fine?
added something
This did not work. I implemented the Comparable interface in my Customer class, as public final class Customer implements Comparable<Customer> as well.
Along with notes of unchecked call, avoiding concrete classes in method signature and variable naming convention following can also be added: Naming type parameters as T instead of AnyType to avoid confusion between a TypeParameter and Class, if Sorting is kind of some utility with just static methods inside it, better to have a private constructor to prevent instantiation, and finally documentation of Comparable.compareTo says It is strongly recommended, but not strictly required that (x.compareTo(y)==0) == (x.equals(y)).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.