2

I'm working on a project where I have customers with reservations on a text file. I'm looking to be able to "listCustomer" them then have them come up in alphabetical order by customer name. All I have right now is:

public static void listCustomers(List<Customer> customers) {  
    System.out.format("\033[31m%s\033[0m%n", "Customers");
    System.out.format("\033[31m%s\033[0m%n", "=========");
    for (Customer c : customers) {
        System.out.format("\033[33m%s\033[0m%n", "Customer"); 
        System.out.format("\033[33m%s\033[0m%n", "--------"); 
        System.out.println(c);
    }       
}   

This works as it does list the customers, but it doesn't list them alphabetically as I'd like them too. I kinda new to coding so if this is a dumb way to do it let me know. Thanks :)

DrewL
  • 23
  • 3
  • [`Collections.sort`](https://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#sort-java.util.List-) – MadProgrammer Dec 08 '15 at 00:57
  • I cannot comment, so please check out: http://stackoverflow.com/questions/708698/how-can-i-sort-a-list-alphabetically – dima Dec 08 '15 at 01:02

3 Answers3

2

You can use the Collections.sort(Comparator<? super Customer>...) method to achieve this.

You only need to implement the Comparator yourself.

import java.util.Comparator;

public class CustomerComparator implements Comparator<Customer> {

    @Override
    public int compare(Customer c1, Customer c2) {
        return c1.toString().compareTo(c2.toString());
    }
}

This will compare the customer's toString() values lexicographically, or in other words, alphabetically, in ascending order. If you need to sort by the customer's name, replace the toString() with the appropriate name property. (Usually getName()). Collections.sort will sort using toString(), so you can simply enter customers.sort() if your toString() method returns the name property.

To print the results:

public static void listCustomers(List<Customer> customers) { 
    CustomerComparator comparator = new CustomerComparator();
    customers.sort(comparator);

    System.out.format("\033[31m%s\033[0m%n", "Customers");
    System.out.format("\033[31m%s\033[0m%n", "=========");
    for (Customer c : customers) {
        System.out.format("\033[33m%s\033[0m%n", "Customer"); 
        System.out.format("\033[33m%s\033[0m%n", "--------"); 
        System.out.println(c);
    }       
}

If you'd like to dispense with an additional class, you can declare the interface inline, like so:

customers.sort(new Comparator<Customer>() {
    public int compare(Customer c1, Customer c2) {
        return c1.toString().compareTo(c2.toString());
    }
});
0
Collections.sort(customers);

//then print with the same logic you have now

nicomp
  • 4,344
  • 4
  • 27
  • 60
0

You can do it with comparator like below.

Main.java

public class Main {

    public static void main(String[] args) {

        List<Customer> customers = new ArrayList<Customer>();

        customers.add(new Customer("001", "Namal"));
        customers.add(new Customer("002", "Lakmini"));
        customers.add(new Customer("003", "Ahas"));

        System.out.println("main().customers 1 : " + customers);

        Collections.sort(customers,new NameSorter());

        System.out.println("main().customers 2 : " + customers);

    }

}

Customer.java

public class Customer {

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

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return this.id + "|" + this.name;
    }

    private String name = null;
    private String id = null;

}

NameSorter.java

public class NameSorter implements Comparator<Customer>{

    @Override
    public int compare(Customer o1, Customer o2) {
        return o1.getName().compareTo(o2.getName());
    }

}
ironwood
  • 8,936
  • 15
  • 65
  • 114