SoFunction
Updated on 2025-03-02

Summary of several methods to implement multiple sorting in Java

introduction

In programming, we often need to sort the data. Java provides a variety of ways to implement sorting, including using () methods, () methods, and Comparable and Comparator interfaces. When multiple sorting is needed, that is, sorting according to multiple fields, we can adopt the following methods:

1. Use () with Comparator

The () method allows us to pass in a Comparator instance to customize the sorting logic. We can implement multi-sorting logic in Comparator.

import .*;
 
public class MultiSortExample {
    public static void main(String[] args) {
        List<Person> people = (
                new Person("John", 25),
                new Person("Alice", 30),
                new Person("Bob", 25),
                new Person("Alice", 22)
        );
 
        (people, new Comparator<Person>() {
            @Override
            public int compare(Person p1, Person p2) {
                int ageCompare = ((), ());
                if (ageCompare != 0) {
                    return ageCompare;
                }
                return ().compareTo(());
            }
        });
 
        for (Person person : people) {
            (() + " " + ());
        }
    }
 
    static class Person {
        private String name;
        private int age;
 
        public Person(String name, int age) {
             = name;
             = age;
        }
 
        public String getName() {
            return name;
        }
 
        public int getAge() {
            return age;
        }
    }
}

2. Use and thenComparing

Java 8 has introducedComparatorInterfacecomparingandthenComparingMethod to make multiple sorting more concise.

import .*;
 
public class MultiSortExample {
    public static void main(String[] args) {
        List<Person> people = (
                new Person("John", 25),
                new Person("Alice", 30),
                new Person("Bob", 25),
                new Person("Alice", 22)
        );
 
        (people, (Person::getAge)
                .thenComparing(Person::getName));
 
        for (Person person : people) {
            (() + " " + ());
        }
    }
 
    static class Person {
        private String name;
        private int age;
 
        public Person(String name, int age) {
             = name;
             = age;
        }
 
        public String getName() {
            return name;
        }
 
        public int getAge() {
            return age;
        }
    }
}

3. Use the Stream API to sort

Java 8'sStreamThe API provides a more modern way to handle collections, including sorting.

import .*;
import .*;
 
public class MultiSortExample {
    public static void main(String[] args) {
        List<Person> people = (
                new Person("John", 25),
                new Person("Alice", 30),
                new Person("Bob", 25),
                new Person("Alice", 22)
        );
 
        List<Person> sortedPeople = ()
                .sorted((Person::getAge)
                        .thenComparing(Person::getName))
                .collect(());
 
        (person -> (() + " " + ()));
    }
 
    static class Person {
        private String name;
        private int age;
 
        public Person(String name, int age) {
             = name;
             = age;
        }
 
        public String getName() {
            return name;
        }
 
        public int getAge() {
            return age;
        }
    }
}

4. Use the Comparable interface

If your class can be controlled, it can be implementedComparableInterface, and incompareToMulti-sorting logic is implemented in the method.

import .*;
 
public class MultiSortExample {
    public static void main(String[] args) {
        List&lt;Person&gt; people = (
                new Person("John", 25),
                new Person("Alice", 30),
                new Person("Bob", 25),
                new Person("Alice", 22)
        );
 
        (null); // Default sort 
        for (Person person : people) {
            (() + " " + ());
        }
    }
 
    static class Person implements Comparable&lt;Person&gt; {
        private String name;
        private int age;
 
        public Person(String name, int age) {
             = name;
             = age;
        }
 
        public String getName() {
            return name;
        }
 
        public int getAge() {
            return age;
        }
 
        @Override
        public int compareTo(Person other) {
            int ageCompare = (, );
            if (ageCompare != 0) {
                return ageCompare;
            }
            return ();
        }
    }
}

in conclusion

Multi-sorting is a common requirement in data processing. Java provides a variety of flexible ways to implement this functionality from traditionalComparatorTo the modernStreamAPI, developers can choose appropriate methods based on specific needs and code style.

This is the end of this article about several methods to implement multiple sorting in Java. For more related content on Java, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!