Java Program to Sort ArrayList of Custom Objects By Property

Updated on December 19, 2024
Sort arraylist of custom objects by property header image

Introduction

Sorting an ArrayList of custom objects by a specific property is a common necessity in Java programming, particularly when dealing with a large set of data that needs organization according to specific criteria. Java offers several ways to achieve this, utilizing comparators and comparable interfaces to tailor sorting mechanisms efficiently.

In this article, you will learn how to adeptly sort an ArrayList of custom objects by property. Explore practical examples using both the Comparable and Comparator interfaces. The examples provided will guide you through coding efficient sorting mechanisms tailored to your Java applications.

Sorting Using Comparable

Implement Comparable in Your Class

  1. Define a custom class that implements the Comparable interface.

  2. Include a compareTo method within your class that defines the sorting logic based on a specific property.

    java
    public class Employee implements Comparable<Employee> {
        private int id;
        private String name;
    
        public Employee(int id, String name) {
            this.id = id;
            this.name = name;
        }
    
        public String getName() {
            return name;
        }
    
        @Override
        public int compareTo(Employee other) {
            return this.name.compareTo(other.name);
        }
    }
    

    In this code, the Employee class implements Comparable with a sorting logic that sorts employees by their name alphabetically.

Sort the ArrayList

  1. Create an ArrayList of Employee objects.

  2. Use the Collections.sort() method to sort the ArrayList.

    java
    import java.util.ArrayList;
    import java.util.Collections;
    
    public class Main {
        public static void main(String[] args) {
            ArrayList<Employee> employees = new ArrayList<>();
            employees.add(new Employee(1, "Alice"));
            employees.add(new Employee(2, "Bob"));
            employees.add(new Employee(3, "Steve"));
    
            Collections.sort(employees);
    
            for(Employee emp : employees) {
                System.out.println(emp.getName());
            }
        }
    }
    

    This snippet creates an ArrayList of Employee objects and sorts them using the Comparable interface implementation. The output will list the names in alphabetical order: Alice, Bob, Steve.

Sorting Using Comparator

Define a Comparator Class

  1. Create a comparator class that implements Comparator<Employee>.

  2. Customize the compare method to sort based on a different attribute, such as employee ID.

    java
    import java.util.Comparator;
    
    public class IdComparator implements Comparator<Employee> {
        @Override
        public int compare(Employee e1, Employee e2) {
            return Integer.compare(e1.getId(), e2.getId());
        }
    }
    

    This code provides a comparator to sort Employee objects by their ID.

Use the Comparator to Sort

  1. Instantiate the Comparator.

  2. Use Collections.sort() with the comparator as a second argument to sort the ArrayList based on ID.

    java
    ArrayList<Employee> employees = new ArrayList<>();
    employees.add(new Employee(3, "Steve"));
    employees.add(new Employee(1, "Alice"));
    employees.add(new Employee(2, "Bob"));
    
    Collections.sort(employees, new IdComparator());
    
    for(Employee emp : employees) {
        System.out.println(emp.getName());
    }
    

    This example sorts the Employee objects by ID, yielding the output: Alice, Bob, Steve, adhering to their ID order.

Conclusion

Sorting an ArrayList of custom objects by property in Java is streamlined by the use of the Comparable and Comparator interfaces. Whether sorting by a natural order using Comparable or implementing multiple custom orderings with Comparator, Java provides robust tools that ensure your data is organized efficiently. By following the examples provided, embrace the flexibility of Java to enhance data handling in your applications, ensuring your lists are always ordered precisely as needed.