Java LinkedList Sort Example

This example shows how to sort the elements of the LinkedList in Java. The example also shows how to sort the LinkedList using the Comparator and Comparable interfaces.

How to sort LinkedList in Java?

We can use the sort method of the Collections class to sort the elements of the LinkedList.

The above method takes a List as an argument. Since the LinkedList class implements the List interface, we can use this method to sort the LinkedList object.


As you can see from the output, the linked list elements are sorted in ascending order by the sort method. The sort method orders the elements in their natural order which is ascending order for the type Integer.

Note: The LinkedList elements must implement the Comparable interface for this method to work. The below given example shows how to do that in a custom class.


As you can see from the output, the compareTo method defines the natural ordering of the Student objects.

How to sort the LinkedList containing custom class objects using a Comparator?

We have seen how to sort the linked list elements using the Comparable interface. In the following example, I will show you how to sort the LinkedList using the Comparator interface. I am going to use the same Student class for this example.


This example is a part of the LinkedList in Java tutorial.

Please let me know your views in the comments section below.

Java 8 LinkedList
Java 8 Collections class

About the author



My name is RahimV and I have over 16 years of experience in designing and developing Java applications. Over the years I have worked with many fortune 500 companies as an eCommerce Architect. My goal is to provide high quality but simple to understand Java tutorials and examples for free. If you like my website, follow me on Facebook and Twitter.

Add Comment

Your email address will not be published. Required fields are marked *

Online Shopping