Java Comparator
Java Comparator
Java Comparator
You have 2 free stories left this month. Sign up and get an extra one for free.
Java Comparator
TyAnthoney Morrell Follow
Mar 20, 2019 · 3 min read
Some of the time in Java it is desirable to sort classes that you have
written yourself. When this is necessary we can use the Java Comparator
Interface to implement our custom sorts.
This is the method that will hold the code you wish to be executed in your
custom sorting.
Let’s assume that we are writing an application that will contain lists of
customers and that you would need to be sorting these customers for
various reasons. We can start out with a Customer class like the one
below:
We can write three different classes for sorting this data: the first would
be for the employee ID;
@Override
public int compare(Customer a, Customer b) {
return a.getCustomerID().compareTo(b.getCustomerID());
@Override
public int compare(Customer a, Customer b) {
return a.getlName().compareTo(b.getlName());
@Override
public int compare(Customer a, Customer b) {
return a.getfName().compareTo(b.getfName());
We can use these classes to sort a list using Collections.sort like in the
main method below
System.out.println("Before sorting");
System.out.println(customer.toString());
// sort by cutomer id
Collections.sort(customers, new CustomerIDSort());
System.out.println("\nSorted by CustomerID");
System.out.println(customer.toString());
System.out.println(customer.toString());
System.out.println(customer.toString());
Output
Before sorting
Customer{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 CustomerID
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}
Customer{customerID=12, fName=Michael, lName=Blonde}
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.
System.out.println("Before sorting");
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("\nSorted by CustomerID");
System.out.println(customer.toString());
return a.getlName().compareTo(b.getlName());
});
System.out.println(customer.toString());
return a.getfName().compareTo(b.getfName());
});
System.out.println(customer.toString());
Before sorting
Customer{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 CustomerID
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}
Customer{customerID=12, fName=Michael, lName=Blonde}
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.
Java Java8
27 claps 3 responses
WRIT T EN BY
TyAnthoney Morrell Follow
6 Stages of Learning a AWS Elastic Beanstalk Making Rust Projects Regex tutorial — A quick
New Programming infrastructure in code with Cargo cheatsheet by examples
Language with Terraform James Bowen Jonny Fox in Factory Mind
Jun Wu in Better Programming Andrey in Seamless Cloud
How to Use a PostGIS Windows Containers on Partial Updates (PATCH) Why is Free Pascal
Geometry With Peewee Kubernetes in Spring Boot better than PHP?
Oren Cohen Tyler Finethy in Better Henrick Kakutalwa Jakub Groncki in T he Startup
Programming