Sorting ArrayList of Java Objects by Field: Complete Guide with Code

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Taming the Chaos: Sorting an ArrayList of Objects by Any Field in Java

\n\n

Imagine: you're writing a book catalog application. You have a Book class with fields title, author, price, and year. You put dozens of books into an ArrayList. Then the user asks: "Show me the newest first!", and a minute later — "No, sort by price!".

\n\n

If you've ever faced this task, you know that simply calling Collections.sort(list) won't work. Java can't read minds. You need to tell it: which specific field to sort by. This is exactly the most popular question on Stack Overflow regarding sorting custom objects.

\n\n

In this article, we'll cover all ways to sort an ArrayList by an object's property: from the classic Comparable to elegant lambda expressions and the Stream API. Let's go!

\n\n

Preparation: The Class We'll Sort

\n\n

For the examples, we'll create a simple Person class:

\n\n
public class Person {\n    private String name;\n    private int age;\n    private double salary;\n\n    public Person(String name, int age, double salary) {\n        this.name = name;\n        this.age = age;\n        this.salary = salary;\n    }\n\n    // Getters (setters omitted for brevity)\n    public String getName() { return name; }\n    public int getAge() { return age; }\n    public double getSalary() { return salary; }\n\n    @Override\n    public String toString() {\n        return name + " (" + age + " years, $" + salary + ")";\n    }\n}\n
\n\n

And populate our list:

\n\n
ArrayList<Person> people = new ArrayList<>();\npeople.add(new Person("Anna", 28, 55000));\npeople.add(new Person("Boris", 35, 72000));\npeople.add(new Person("Victor", 22, 48000));\npeople.add(new Person("Galina", 30, 62000));\n
\n\n

Now everything is ready for experiments. How to sort this list by age? By name? By salary? Java has at least three ways. Let's start with the fundamental one.

\n\n

Method 1: Comparable — Embedding Sorting Logic Directly into the Class

\n\n

The Comparable interface defines the "natural order" of an object. If you want your objects to always be sorted by one field (e.g., age), implement this interface.

\n\n

Let's modify our Person class:

\n\n
public class Person implements Comparable<Person> {\n    // ... fields, constructor, getters ...\n\n    @Override\n    public int compareTo(Person other) {\n        return Integer.compare(this.age, other.age);\n    }\n}\n
\n\n

Now sorting is one line:

\n\n
Collections.sort(people);\nSystem.out.println(people);\n// Output: [Victor (22 years, $48000), Anna (28 years, $55000), Galina (30 years, $62000), Boris (35 years, $72000)]\n
\n\n

Pros: simplicity, no need to think about external comparators.
\nCons: you "hardcode" only one sorting method into the class. If tomorrow you need to sort by name, you'll have to change the class.

\n\n

By the way, for reverse order (largest to smallest), you can use Collections.reverseOrder():

\n\n
Collections.sort(people, Collections.reverseOrder());\n
\n\n

Method 2: Comparator — Flexibility Without Changing the Class

\n\n

When you need to sort differently in different parts of the program, Comparator comes to the rescue. It's a separate class or anonymous class that contains the comparison logic.

\n\n

Anonymous Class — The Old-School Method

\n\n
Collections.sort(people, new Comparator<Person>() {\n    @Override\n    public int compare(Person p1, Person p2) {\n        return p1.getName().compareTo(p2.getName());\n    }\n});\n
\n\n

It works, but looks bulky. In

Blogs

Book Recommendations