-2

While sorting an arraylist of Customer Class(user defined) having name and age as attributes on the basis of name, Collections.sort() method is showing error that "the type java.util.Comparator is not resolved.it is indirectly referenced from required .class file.

package comparable;
import java.util.*;

public class Tester {
    public static void main(String[] args){
        List<Customer> custtlist=new ArrayList<Customer>();
        Customer c1=new Customer("vikas",1);
        Customer c2=new Customer("mittal",2);
        custtlist.add(c1);
        custtlist.add(c2);

        System.out.println("Before Sorting");
        Iterator<Customer> iterator = custtlist.iterator();
        while(iterator.hasNext()){
            Customer customer = (Customer) iterator.next();
            System.out.println(customer.getCustname());
        }
        Collections.sort(custtlist);   


        System.out.println("After Sorting");
        iterator = custList.iterator();

        while (iterator.hasNext()) {
            Customer customer = (Customer) iterator.next();
            System.out.println(customer.getCustName());
        }
    }
}

//Customer Class
package comparable;

public class Customer implements Comparable<Customer> {
    private String custname;
    private int age;

    public Customer(String custname, int age) {
        this.custname = custname;
        this.age = age;
    }

    public Customer() {
    }

    public String getCustname() {
        return custname;
    }

    public void setCustname(String custname) {
        this.custname = custname;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int compareTo(Customer c){
        return this.custname.compareTo(c.getCustname());
    }
}
Sergey Gornostaev
  • 7,596
  • 3
  • 27
  • 39
Vik Mittal
  • 71
  • 1
  • 2
  • 6
  • Maybe your Customer.compareTo method is not implemented well. – Alireza Mohamadi May 07 '16 at 16:55
  • Could you please add your relevant code? – Mad Matts May 07 '16 at 16:55
  • I've tested your code and it works. You have a pair of typos in it "iterator = custList.iterator();" should be "iterator = custtlist.iterator();" and "System.out.println(customer.getCustName());" should be "System.out.println(customer.getCustname());". Sort method works as expected. – RubioRic May 08 '16 at 06:40

1 Answers1

2

You have an awful lot of typos in the code. After I corrected them, the compilation was successful. I took the liberty to make some improvements and styling.

import java.util.*;

public class Tester {
    public static void main(String[] args){
        List<Customer> customersList = new ArrayList<Customer>();
        Customer c1 = new Customer("vikas", 1);
        Customer c2 = new Customer("mittal", 2);
        customersList.add(c1);
        customersList.add(c2);

        System.out.println("Before Sorting");
        for(Customer customer : customersList) {
            System.out.println(customer.getName());
        }

        Collections.sort(customersList);
        System.out.println("\nAfter Sorting");

        for(Customer customer : customersList) {
            System.out.println(customer.getName());
        }
    }
}

public class Customer implements Comparable<Customer> {
    private String name;
    private int age;

    public Customer(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public Customer() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int compareTo(Customer c){
        return this.name.compareTo(c.getName());
    }
}
Sergey Gornostaev
  • 7,596
  • 3
  • 27
  • 39