📊 Java Data Structures
Estimated reading: 4 minutes 329 views

Java List Sorting – Complete Guide with Syntax, Comparators & Best Practices


Introduction – Why List Sorting Matters in Java

Whether you’re building a shopping cart, leaderboard, or contact list — sorting is essential. Java offers multiple ways to sort a list using built-in methods and custom logic for advanced ordering.

With the power of the Collections Framework, Lambdas, and Comparators, Java makes it easy to sort lists by names, numbers, dates, or even complex objects.

By the end of this guide, you’ll learn:

How to sort a Java List in ascending and descending order
How to use Collections.sort() and List.sort()
How to sort custom objects with Comparator and Lambdas
Performance tips and best practices


What Is List Sorting in Java?

Sorting a List in Java means arranging its elements in a specific order (natural or custom), using utilities from the Collections or List interface.


1. Sorting a List of Strings

import java.util.*;

public class SortStrings {
    public static void main(String[] args) {
        List<String> names = Arrays.asList("John", "Alice", "Bob");
        Collections.sort(names); // Ascending
        System.out.println(names); // [Alice, Bob, John]
    }
}

Collections.sort() sorts the list in natural (alphabetical) order


Descending Order

names.sort(Collections.reverseOrder());
System.out.println(names); // [John, Bob, Alice]

Use reverseOrder() for descending sort


2. Sorting a List of Integers

List<Integer> numbers = Arrays.asList(5, 1, 3, 9);
Collections.sort(numbers);
System.out.println(numbers); // [1, 3, 5, 9]

Java uses natural order (ascending) for numbers


3. Sorting Custom Objects (e.g., Students)

Using Comparator Class

class Student {
    String name;
    int age;

    Student(String n, int a) {
        name = n;
        age = a;
    }

    public String toString() {
        return name + " (" + age + ")";
    }
}
List<Student> list = new ArrayList<>();
list.add(new Student("Ravi", 23));
list.add(new Student("Aman", 21));
list.add(new Student("Sneha", 22));

// Sort by age
list.sort(Comparator.comparingInt(s -> s.age));
System.out.println(list); // [Aman (21), Sneha (22), Ravi (23)]

Comparator.comparingInt() is concise and readable
You can also use Collections.sort(list, comparator)


Sort by Name (String Field)

list.sort(Comparator.comparing(s -> s.name));

Alphabetical sorting by name using Lambda


Sort in Descending Order

list.sort(Comparator.comparingInt((Student s) -> s.age).reversed());

Add .reversed() to any comparator


Collections.sort() vs List.sort()

MethodAvailable SinceSourceNotes
Collections.sort()Java 1.2java.util.CollectionsStatic method, requires List as parameter
List.sort()Java 8List interfaceUses lambda, cleaner, more modern

4. Sorting with Streams (Java 8+)

list.stream()
    .sorted(Comparator.comparing(s -> s.name))
    .forEach(System.out::println);

Non-destructive sort — use when you don’t want to modify original list


Real-World Use Case: Sorting Products by Price

class Product {
    String name;
    double price;

    Product(String name, double price) {
        this.name = name;
        this.price = price;
    }

    public String toString() {
        return name + ": $" + price;
    }
}

List<Product> products = Arrays.asList(
    new Product("Phone", 799.99),
    new Product("Laptop", 1200.0),
    new Product("Tablet", 450.0)
);

products.sort(Comparator.comparingDouble(p -> p.price));
System.out.println(products);

Sorts the product list by price in ascending order


Best Practices for List Sorting

Use Comparator.comparing() and Lambdas for cleaner code
Prefer List.sort() for modern Java (Java 8+)
Use .reversed() to flip sorting order
Use stream().sorted() for non-destructive sorting
Avoid null values unless explicitly handled in comparator


Summary

  • Java provides powerful tools to sort any type of List
  • Use List.sort() or Collections.sort() for in-place sorting
  • Leverage Comparator, Lambdas, and Streams for custom sorting logic
  • Use .reversed(), thenComparing(), and nullsFirst() for advanced control

FAQs – Java List Sorting

How do I sort a list in descending order?

Use .sort(Collections.reverseOrder()) or Comparator.reversed()

What if the list contains null values?

Use a comparator like:

Comparator.nullsLast(Comparator.comparing(...))

Can I sort by multiple fields?

Yes. Use thenComparing():

list.sort(Comparator.comparing(Student::getAge).thenComparing(Student::getName));

Does sorting modify the original list?

Yes — Collections.sort() and List.sort() sort in place.
Use Streams if you want to keep the original list unchanged.

What is the default sort order?

Java sorts strings alphabetically and numbers ascendingly by default.


Share Now :
Share

Java List Sorting

Or Copy Link

CONTENTS
Scroll to Top