Java Comparator

Sorted by first name"); for (Customer customer : customers) { System.

out.

println(customer.

toString()); } }OutputBefore sortingCustomer{customerID=4, fName=Steve, lName=Pink}Customer{customerID=3, fName=Tim, lName=Orange}Customer{customerID=1, fName=Edward, lName=Blue}Customer{customerID=9, fName=Harvey, lName=White}Customer{customerID=2, fName=Quentin, lName=Brown}Customer{customerID=12, fName=Michael, lName=Blonde}Sorted by CustomerIDCustomer{customerID=1, fName=Edward, lName=Blue}Customer{customerID=2, fName=Quentin, lName=Brown}Customer{customerID=3, fName=Tim, lName=Orange}Customer{customerID=4, fName=Steve, lName=Pink}Customer{customerID=9, fName=Harvey, lName=White}Customer{customerID=12, fName=Michael, lName=Blonde}Sorted by last nameCustomer{customerID=12, fName=Michael, lName=Blonde}Customer{customerID=1, fName=Edward, lName=Blue}Customer{customerID=2, fName=Quentin, lName=Brown}Customer{customerID=3, fName=Tim, lName=Orange}Customer{customerID=4, fName=Steve, lName=Pink}Customer{customerID=9, fName=Harvey, lName=White}Sorted by first nameCustomer{customerID=1, fName=Edward, lName=Blue}Customer{customerID=9, fName=Harvey, lName=White}Customer{customerID=12, fName=Michael, lName=Blonde}Customer{customerID=2, fName=Quentin, lName=Brown}Customer{customerID=4, fName=Steve, lName=Pink}Customer{customerID=3, fName=Tim, lName=Orange}If you are using Java 8 you can actually sort the above list or any other list for that matter using a lambda expression instead of a class.

Here is how our main method would look if we were to use lamda expressions.

public static void main(String[] args) { List<Customer> customers = new ArrayList<>(); customers.

add(new Customer(4, "Steve", "Pink")); customers.

add(new Customer(3, "Tim", "Orange")); customers.

add(new Customer(1, "Edward", "Blue")); customers.

add(new Customer(9, "Harvey", "White")); customers.

add(new Customer(2, "Quentin", "Brown")); customers.

add(new Customer(12, "Michael", "Blonde")); System.

out.

println("Before sorting"); for (Customer customer : customers) { System.

out.

println(customer.

toString()); } // sort by cutomer id Collections.

sort(customers, (Customer a, Customer b) -> { return a.

getCustomerID().

compareTo(b.

getCustomerID()); }); System.

out.

println(".Sorted by CustomerID"); for (Customer customer : customers) { System.

out.

println(customer.

toString()); } // sort by last name Collections.

sort(customers, (Customer a, Customer b) -> { return a.

getlName().

compareTo(b.

getlName()); }); System.

out.

println(".Sorted by last name"); for (Customer customer : customers) { System.

out.

println(customer.

toString()); } // sort by first name Collections.

sort(customers, (Customer a, Customer b) -> { return a.

getfName().

compareTo(b.

getfName()); }); System.

out.

println(".Sorted by first name"); for (Customer customer : customers) { System.

out.

println(customer.

toString()); } }The output is the sameBefore sortingCustomer{customerID=4, fName=Steve, lName=Pink}Customer{customerID=3, fName=Tim, lName=Orange}Customer{customerID=1, fName=Edward, lName=Blue}Customer{customerID=9, fName=Harvey, lName=White}Customer{customerID=2, fName=Quentin, lName=Brown}Customer{customerID=12, fName=Michael, lName=Blonde}Sorted by CustomerIDCustomer{customerID=1, fName=Edward, lName=Blue}Customer{customerID=2, fName=Quentin, lName=Brown}Customer{customerID=3, fName=Tim, lName=Orange}Customer{customerID=4, fName=Steve, lName=Pink}Customer{customerID=9, fName=Harvey, lName=White}Customer{customerID=12, fName=Michael, lName=Blonde}Sorted by last nameCustomer{customerID=12, fName=Michael, lName=Blonde}Customer{customerID=1, fName=Edward, lName=Blue}Customer{customerID=2, fName=Quentin, lName=Brown}Customer{customerID=3, fName=Tim, lName=Orange}Customer{customerID=4, fName=Steve, lName=Pink}Customer{customerID=9, fName=Harvey, lName=White}Sorted by first nameCustomer{customerID=1, fName=Edward, lName=Blue}Customer{customerID=9, fName=Harvey, lName=White}Customer{customerID=12, fName=Michael, lName=Blonde}Customer{customerID=2, fName=Quentin, lName=Brown}Customer{customerID=4, fName=Steve, lName=Pink}Customer{customerID=3, fName=Tim, lName=Orange}That is it.

We can now use what we have learned to write useful compare methods to sort any type of class that we can think of.

I hope this brief summary of the Comparator Interface was helpful and should you like to view the source code for this tutorial it is available on GitHub.

.

. More details

Leave a Reply