The list is a data structure in Java that maintains the ordered collection of data. An index of the element declared in the list is a way to retrieve, update, and delete the elements. The list in Java is created by declaring an object. Different methods and functions are available in Java which are implemented while working with the list according to the situation. One such situation is to sort the list in Java according to the requirement of the code.
In this article, the topics listed below will be covered with a practical demonstration:
- Why There is a Need to Sort a List?
- How to sort a List in Java?
- Approach 1: Using list.sort() Method
- Approach 2: Using Stream.sort() Method
- Approach 3: Using Stream.sort with collectors
- Approach 4: Using collections.sort() Method
- Approach 5: Using collections.reverseorder()Method
- Approach 6: Using comparator.naturalorder() Method
- Approach 7: Using comparator.reverseorder() Method
- Conclusion
Why There is a Need to Sort a List?
Sorting simplifies the data and makes it easier to analyze something based on the sorted data. Sorting can be helpful in many real-life scenarios. Let us take an example of a University where the list of students is sorted on the basis of the highest grades obtained. Another example can be the supermarket where the list of items is printed on the basis of prices, or on the basis of brands. The sorting in Java can be done using different criteria. It can be done on the basis of different objects, numbers, and integers.
How to Sort a List in Java?
In Java, the term “sort” refers to “put the data in order” be it in alphabetic or numeric order. The ascending order and the descending order are the two ways to display a list in Java. This sorting can be applied to the data structures present in Java. One of the data structures that uses different methods for sorting is a list in Java. There are multiple methods to sort a list in Java. These methods are listed below.
- Approach 1: Using list.sort() Method
- Approach 2: Using Stream.sort() Method
- Approach 3: Using Stream.sort with collectors
- Approach 4: Using collections.sort() Method
- Approach 5: Using collections.reverseorder()Method
- Approach 6: Using comparator.naturalorder() Method
- Approach 7: Using comparator.reverseorder() Method
Approach 1: Using list.sort() Method
The list.sort() method in Java sorts the list according to the natural order. The code below implements the list.sort() method in Java.
//Import the package required
import java.util.*;
//Declare the class
public class SortExample1
{ //The main() method of Java
public static void main(String[] args)
{
// List of Integers
List<Integer> list2 = Arrays.asList(10, 12, -5, 40, -8, 25, 1);
System.out.println("The Original List is: " + list2);
//Method to sort the elements
list2.sort(null);
System.out.println("The Sorted List is: " + list2);
}
}
In the above Java code:
- The package is imported and the class is declared in Java.
- The list of integers is created using the asList() method of Java.
- The list of integers is sorted in natural order using the list.sort(null) method in Java.
- The sorted list of integers is printed as the output.
Output
The output below shows/prints the original list and the sorted list.

Approach 2: Stream.sort() Method
The Stream.sort() method sorts the integer list in a natural order. The syntax along with the code is implemented below to understand this method.
Stream<T> sorted()
In the above syntax, T is the type of elements present in the stream.
//Import the required package
import java.util.*;
//Declare the class
class SortExample2 {
// main() method
public static void main(String[] args)
{ // Declare a list of strings
List<String> list1 = Arrays.asList("Hello", "to", "the", "world", "of", "Java");
System.out.println("The Stream is sorted as : ");
//Stream sorted in natural order
list1.stream().sorted().forEach(System.out::println);
}
}
In the above code block:
- The java.util package is imported.
- The class in the next step is declared as “SortExample2”.
- A list object is created as “list1” and the strings are added to the list.
- The stream.sorted() method of Java sorts the list on the basis of the natural order of the alphabet and each element of the list is printed separately.
Output
The output below shows that the sorted list of elements is printed accordingly.

Approach 3: Using Stream.sorted() With Collectors
The Stream.sorted() method with collections in Java returns a sorted stream of the elements in Java. Here is a practical demonstration of the stated method.
//Import the required package
import java.util.*;
import java.util.stream.*;
//Declare the class
class SortExample3 {
// main() method
public static void main(String[] args)
{ // Declare a List of strings
List<String> list1 = Arrays.asList("Hello", "to", "the", "world", "of", "Java");
System.out.println("The Stream is sorted as : ");
//Stream sorted in the natural order
List<String> list2 = list1.stream().sorted().collect(Collectors.toList());
list2.forEach(System.out::println);
}
}
In the above code block:
- The same list is declared as the previous example.
- The list.sorted() sorts the list along with the collect() method that collects the elements from the stream and then stores these elements in the collection.
- The toList() method returns all the declared elements from the collectors to the list.
- The “for each” loop of Java prints the sorted elements separately.
Output
The list of strings is sorted with respect to the alphabetic order as depicted in the output below.

Approach 4: Using Collection.sort() Method of Java
The collections.sort() method of the java.util.Collections class returns the sorted list of elements in ascending order by default. The syntax along with the code is implemented below.
Collections.sort(list);
In the syntax declared above:
- The list of elements is declared.
- No value is returned by sort().
Let’s implement the collections.sort() method practically:
//Import the package required
import java.util.*;
//Declare the class
public class SortExample4
{ //The main() method of Java
public static void main(String[] args)
{
// List of strings
List<String> list2 = Arrays.asList("Coding", "in", "Java", "is", "fun");
System.out.println("The Original List is: " + list2);
//Method to sort the elements
Collections.sort(list2);
// Let us print the sorted list
System.out.println("The List after sorting is: " + list2);
}
}
In the above code block:
- The required package is imported and the class is declared as “SortExample4”.
- The strings are entered into the List for sorting.
- The collections.sort() method of Java sorts the elements present in the list.
Output
The output below shows that the elements in the original list and the sorted list are printed accordingly in ascending order using the collections.sort() method of Java.

Approach 5: Using collections.reverseorder() in Java
This method returns the declared elements in reverse order after the elements of the list are sorted using the collections.sort() method of Java. The example below is implemented using the strings in Java.
//Import the package required
import java.util.*;
//Declare the class
public class SortExample5
{ //The main() method of Java
public static void main(String[] args)
{
// Enter the strings to the List
List<String> list2 = Arrays.asList("Coding", "in", "Java", "is", "fun");
System.out.println("The Original List is: " + list2);
//Method to sort the elements
Collections.sort(list2);
System.out.println("The Sorted List is: " + list2);
Collections.sort(list2, Collections.reverseOrder());
//print the sorted list
System.out.println("The List after sorting is: " + list2);
}
}
In the above code block:
- The string List is declared in Java and strings are inserted into the list.
- Using the collections.sort() methods the strings in the ArrayList are sorted and printed using the println() method.
- This method In Java prints the sorted elements in reverse order using the collections.reverseorder().
Output
The output below shows that the elements are sorted and printed using the collections.sort() method.

To get more clarity of the concept let us implement on the Integer list also using the asList() method as shown below.
//Import the package required
import java.util.*;
//Declare the class
public class SortExample5
{ //The main() method of Java
public static void main(String[] args)
{
// List of Integers
List<Integer> list2 = Arrays.asList(10, 12, -5, 40, -8, 25, 1);
System.out.println("The Original List is: " + list2);
//Method to sort the elements
Collections.sort(list2);
System.out.println("The Sorted List is: " + list2);
Collections.sort(list2, Collections.reverseOrder());
// Let us print the sorted list
System.out.println("The List after sorting is: " + list2);
}
}
In the above code block:
- Using the asList() method, the Integers are declared in the list in Java.
- The collections.reverse() order along with the sort() method sorts the integer list first and then it returns the list in reverse order.
Output
The Original integer list, the sorted list, and the list in the reverse order are printed.

Approach 6: The Comparator.naturalOrder() Method
The comparator.naturalOrder() method in Java returns a comparator that compares the objects present in the list and returns the sorted list in the natural order. The syntax of this method is implemented below along with the code.
Static <T extends Comparable <? super T>> Comparator<T> naturalOrder()
In the syntax above
- “T” is the comparable type of the element that is to be compared.
- The elements are printed in the “naturalOrder” as provided in the list.
//Import the package required
import java.util.*;
import java.util.stream.*;
//Declare the class
public class SortExample6
{ //The main() method of Java
public static void main(String[] args)
{
// Enter the integers to the List
List<Integer> list2 = Arrays.asList(10, 12, -5, 40, -8, 25, 1);
System.out.println("The Original List is: " + list2);
list2.sort(Comparator.naturalOrder());
System.out.println("The Sorted List is: " + list2);
}
}
In the above Java code:
- The package required for the method is imported.
- The class in Java is declared as “SortExample5”.
- In the next step, the integers are entered into the list.
- The sort() method along with the Comparator.naturalorder() method returns the list in the sorted natural order in Java.
- The original and the sorted list are printed as the output.
Output
The output below shows that the original and sorted integer list is printed

Approach 7: The Comparator.reverseOrder() Method
The comparator.reverseOrder() compares the value using a comparator and returns the value present in the list in reverse order. Let’s implement this method practically.
//Import the package required
import java.util.*;
import java.util.stream.*;
//Declare the class
public class SortExample7
{ //The main() method of Java
public static void main(String[] args)
{
// Declare Integers to the List
List<Integer> list2 = Arrays.asList(10, 12, -5, 40, -8, 25, 1);
System.out.println("The Original List is: " + list2);
list2.sort(Comparator.reverseOrder());
System.out.println("The Sorted List in Reverse Order is: " + list2);
}
}
In the above Java code, the Comparator.reverseOrder() method in Java returns the sorted list in reverse order.
Output
The output below shows that the elements of the original list are printed along with the sorted elements in the reverse order. The negative integer values are printed at the end and the positive integer values are printed at the start of the list.

This concludes the working of different methods to sort a list in Java.
Conclusion
To sort a list in Java there are various methods available. These methods include list.sort(), Stream.sort(), Stream.sorted with collectors, collections.sort(), collections.reverseorder(), comparator.naturalorder(), and comparator.reverseorder() Method. The simplest method among all these methods is the list.sort() method of Java. In this article, we have sorted the list using multiple methods in Java.