Home Java LinkedList Methods sort() Java LinkedList sort() Method 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.
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] Call What 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.util.LinkedList (default method from java.util.List)
public void sort(Comparator<? super E> c) Parameters Parameter Description c (Comparator<? super E>)The ordering rule, or null for the natural order.
Return Value Return Value Description voidNothing is returned. The list itself is sorted.
Important
This method needs Java 8 or later. With null as the argument, elements that are not Comparable cause a ClassCastException, and null elements cause a NullPointerException. For a LinkedList, Java copies the elements into an array, sorts the array, and writes them back, so sorting costs about the same as for an ArrayList.
Compare with Similar Methods Ways to sort or order a list:
Java Examples 1. Natural order 2. Reverse and by length 3. Sort objects Example 1 — Sort Strings and Numbers in Natural Order Sort a list of words alphabetically and a list of numbers ascending with sort(null).
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);
}
} words: [Fig, apple, banana, pear]
nums: [4, 15, 30, 100] How It Works null means natural order. Strings compare alphabetically, and integers compare by value.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".The rest are alphabetical. After "Fig" come apple, banana and pear.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.
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);
}
} descending: [plum, kiwi, fig, date, banana]
by length: [fig, plum, kiwi, date, banana] How It Works Five words. Three of them have four letters: kiwi, plum and date.reverseOrder. The first sort puts the words in descending alphabetical order, from plum down to banana.comparing(String::length). The second sort orders by the number of letters, so "fig" is first and "banana" is last.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.
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);
}
} price, then name: [book=15, chair=40, lamp=40, desk=120]
price, highest first: [desk=120, chair=40, lamp=40, book=15] How It Works A small class. Each Product has a name and a price, and toString makes the output readable.A two-level comparator. comparingInt orders by price, and thenComparing breaks ties by name.Equal prices are ordered by name. "chair" and "lamp" both cost 40, and "chair" comes first alphabetically.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 How do I sort a LinkedList in Java? Call list.sort(null) for natural order, or list.sort(comparator) for a custom order. You can also call Collections.sort(list).
How do I sort a LinkedList in descending order? Use list.sort(Comparator.reverseOrder()), or list.sort(Collections.reverseOrder()).
Which Java version added list.sort()? Java 8. Before that, you had to use Collections.sort(list).
Is LinkedList sort stable? Yes. Elements that compare as equal keep their original relative order.
Does sort() return a new list? No. It sorts the existing list in place and returns nothing. Make a copy first if you need to keep the original order.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion