SoFunction
Updated on 2025-05-21

Detailed explanation of Java List sorting example code

1. Natural sorting

Natural sorting is sorting in the natural order of objects, such as the size of numbers or the dictionary order of strings. For implementationComparableInterface classes can be used directly()or()Methods to sort.

import ;
import ;
import ;

public class NaturalSortExample {
    public static void main(String[] args) {
        List<Integer> numbers = new ArrayList<>();
        (5);
        (2);
        (8);
        (1);

        // Sort with ()        (numbers);
        ("Sorted list of numbers: " + numbers);

        List<String> words = new ArrayList<>();
        ("apple");
        ("banana");
        ("cherry");

        // Sort with ()        (null);
        ("Sorted word list: " + words);
    }
}

2. Custom sorting rules

When you need to sort by specific rules, you can achieveComparatorInterface customizes the sorting rules.

import ;
import ;
import ;
import ;

public class CustomSortExample {
    public static void main(String[] args) {
        List<Person> people = new ArrayList<>();
        (new Person("Alice", 30));
        (new Person("Bob", 25));
        (new Person("Charlie", 35));

        // Sort by ascending order of age        (people, new Comparator<Person>() {
            @Override
            public int compare(Person p1, Person p2) {
                return ((), ());
            }
        });

        ("Sorted by age:");
        for (Person person : people) {
            (person);
        }

        // Sort by descending age        (people, new Comparator<Person>() {
            @Override
            public int compare(Person p1, Person p2) {
                return ((), ());
            }
        });

        ("Sorted by age descending:");
        for (Person person : people) {
            (person);
        }
    }
}

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;
    }

    @Override
    public String toString() {
        return "Person{name='" + name + "', age=" + age + '}';
    }
}

3. Use Lambda expressions to simplify Comparator

Starting with Java 8, you can use Lambda expressions to simplifyComparatorImplementation.

import ;
import ;
import ;
import ;

public class LambdaSortExample {
    public static void main(String[] args) {
        List<Person> people = new ArrayList<>();
        (new Person("Alice", 30));
        (new Person("Bob", 25));
        (new Person("Charlie", 35));

        // Sort by ascending age using Lambda expressions        ((p1, p2) -> ((), ()));

        ("Sorted by age:");
        (::println);

        // Sort by descending age using Lambda expressions        ((p1, p2) -> ((), ()));

        ("Sorted by age descending:");
        (::println);
    }
}

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;
    }

    @Override
    public String toString() {
        return "Person{name='" + name + "', age=" + age + '}';
    }
}

4. Multi-condition sorting

You can sort it in combination with multiple conditions, such as sorting by age first and then by name.

import ;
import ;
import ;

public class MultiConditionSortExample {
    public static void main(String[] args) {
        List<Person> people = new ArrayList<>();
        (new Person("Alice", 30));
        (new Person("Bob", 25));
        (new Person("Charlie", 30));
        (new Person("David", 25));

        // Ascending order by age, if the age is the same, sort by name dictionary order        ((Person::getAge)
                .thenComparing(Person::getName));

        ("Ascending order by age, same age, sort by name:");
        (::println);
    }
}

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;
    }

    @Override
    public String toString() {
        return "Person{name='" + name + "', age=" + age + '}';
    }
}

5. Summary

Java provides a variety of ways to sort Lists, including natural and custom sorting. By implementingComparableInterface or useComparator, can flexibly define sorting rules. Java 8's Lambda expressions further simplify sorting code, making the code more concise and easy to read.

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