Java LinkedList sort() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 8+

Definition

In Java, sort(comparator) rearranges the elements of the LinkedList into the order given by a Comparator. Passing null means "use the natural order", such as alphabetical order for strings and ascending order for numbers.

The sort happens in place and the method returns nothing. It is stable, which means that elements that compare as equal keep their original relative order.

An instance method of java.util.LinkedList (since Java 8): call it on a LinkedList object. Add import java.util.LinkedList; at the top of your file. It is declared by List, and LinkedList implements it. sort() was added in Java 8.

Syntax

Pass null for natural order, or a Comparator.

Java
LinkedList<String> list = new LinkedList<>(Arrays.asList("pear", "fig", "apple"));
list.sort(null);                              // [apple, fig, pear]
list.sort(Comparator.reverseOrder());         // [pear, fig, apple]
list.sort(Comparator.comparing(String::length)); // [fig, pear, apple]
CallWhat it does
list.sort(null)Natural order (ascending).
list.sort(Comparator.reverseOrder())Descending natural order.
list.sort(Comparator.comparing(f))Order by a value taken from each element.
Collections.sort(list)The older way; it calls list.sort(null).

Natural order needs elements that implement Comparable, such as String and Integer.

Library declarations
Java
// java.util.LinkedList (default method from java.util.List)
public void sort(Comparator<? super E> c)

Parameters

ParameterDescription
c (Comparator<? super E>)The ordering rule, or null for the natural order.

Return Value

Return ValueDescription
voidNothing is returned. The list itself is sorted.

Compare with Similar Methods

Ways to sort or order a list:

MethodHow it differs
Collections.sort(list)The pre-Java 8 way; it simply calls list.sort(null).
Collections.reverse(list)Flips the current order without comparing anything.
replaceAll(op)Changes element values instead of their order.
ArrayList.sort()The same default method on an array-backed list.

Java Examples

Example 1 — Sort Strings and Numbers in Natural Order

Sort a list of words alphabetically and a list of numbers ascending with sort(null).

Java
import java.util.Arrays;
import java.util.LinkedList;

public class LinkedListSortNatural {
    public static void main(String[] args) {
        LinkedList<String> words = new LinkedList<>(
            Arrays.asList("pear", "apple", "Fig", "banana"));
        words.sort(null);
        System.out.println("words: " + words);

        LinkedList<Integer> nums = new LinkedList<>(Arrays.asList(30, 4, 100, 15));
        nums.sort(null);
        System.out.println("nums:  " + nums);
    }
}

How It Works

  1. null means natural order. Strings compare alphabetically, and integers compare by value.
  2. Capital letters sort first. "Fig" starts with an upper-case F, whose character code is smaller than any lower-case letter, so it comes before "apple".
  3. The rest are alphabetical. After "Fig" come apple, banana and pear.
  4. Numbers sort by value. 100 comes last because it is the largest number, even though its text starts with 1.

Example 2 — Sort in Reverse and by Word Length

Sort descending with Comparator.reverseOrder() and by length with Comparator.comparing, and see the stable tie order.

Java
import java.util.Arrays;
import java.util.Comparator;
import java.util.LinkedList;

public class LinkedListSortReverse {
    public static void main(String[] args) {
        LinkedList<String> words = new LinkedList<>(
            Arrays.asList("kiwi", "fig", "banana", "plum", "date"));

        words.sort(Comparator.reverseOrder());
        System.out.println("descending: " + words);

        words.sort(Comparator.comparing(String::length));
        System.out.println("by length:  " + words);
    }
}

How It Works

  1. Five words. Three of them have four letters: kiwi, plum and date.
  2. reverseOrder. The first sort puts the words in descending alphabetical order, from plum down to banana.
  3. comparing(String::length). The second sort orders by the number of letters, so "fig" is first and "banana" is last.
  4. Stable ties. The four-letter words keep the order they had before this sort, which is plum, kiwi, date.

Example 3 — Sort Objects by a Field

Sort a list of products by price, and then by name for products with the same price.

Java
import java.util.Comparator;
import java.util.LinkedList;

public class LinkedListSortObjects {
    static class Product {
        final String name;
        final int price;

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

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

    public static void main(String[] args) {
        LinkedList<Product> items = new LinkedList<>();
        items.add(new Product("lamp", 40));
        items.add(new Product("desk", 120));
        items.add(new Product("book", 15));
        items.add(new Product("chair", 40));

        items.sort(Comparator.comparingInt((Product p) -> p.price).thenComparing(p -> p.name));
        System.out.println("price, then name: " + items);

        items.sort((a, b) -> b.price - a.price);
        System.out.println("price, highest first: " + items);
    }
}

How It Works

  1. A small class. Each Product has a name and a price, and toString makes the output readable.
  2. A two-level comparator. comparingInt orders by price, and thenComparing breaks ties by name.
  3. Equal prices are ordered by name. "chair" and "lamp" both cost 40, and "chair" comes first alphabetically.
  4. A lambda comparator. The second sort puts the highest price first, and because the sort is stable, chair and lamp keep their earlier order.

Frequently Asked Questions

Call list.sort(null) for natural order, or list.sort(comparator) for a custom order. You can also call Collections.sort(list).
Use list.sort(Comparator.reverseOrder()), or list.sort(Collections.reverseOrder()).
Java 8. Before that, you had to use Collections.sort(list).
Yes. Elements that compare as equal keep their original relative order.
No. It sorts the existing list in place and returns nothing. Make a copy first if you need to keep the original order.

Next: spliterator()

Get a Spliterator, the traversal tool used by Java streams.

spliterator() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful