Home » How to Sort a List in Java

How to Sort a List in Java

by Online Tutorials Library

How to sort a list in Java

We can use the following methods to sort the list:

  • Using stream.sorted() method
  • Using Comparator.reverseOrder() method
  • Using Comparator.naturalOrder() method
  • Using Collections.reverseOrder() method
  • Using Collections.sort() method

Java Stream interface

Java Stream interface provides two methods for sorting the list:

sorted() method

Stream interface provides a sorted() method to sort a list. It is defined in Stream interface which is present in java.util package. It returns a stream sorted according to the natural order. If the elements are not comparable, it throws java.lang.ClassCastException. The signature of the method is:

Parameter

T: It is a type of stream element.

Java Stream.sorted(Comparator comparator)

It also returns a stream sorted according to the provided comparator. It is stable for an ordered stream. The signature of the method is:

Parameters

  • T is the type of stream element.
  • comparator to be used to compare elements

Example

In the following example, we have used the following methods:

  • In Java 8, stream() is an API used to process collections of objects.
  • The collect() method is used to receive elements from a stream and stored them in a collection.
  • The toList() return the collector which collects all the input elements into a list, in encounter order.

Example

Output:

How to sort a list in Java


Java Comparator.reverseOrder() method

The reverseOrder() is a method of Comparator interface which is defined in java.util package. The method returns a comparator that imposes the reverse of the natural ordering. It throws NullPointerException when comparing null. The method signature is:

Comparable is also an interface belong to a java.lang package.

Parameters

T: comparable type of element to be compared.

Example

Output:

How to sort a list in Java


Java Comparator.naturalOrder()

The method returns a comparator that compares Comparable objects in the natural order. The returned comparable is serializable. When we compare null, it throws NullPointerException. It is from Java 8. The signature of the method is:

  • The method sorts the elements in natural order (ascending order).
  • It puts null at the top if any.
  • It puts the capital letter elements first in natural order after that small letters in the natural order, if the list has both small and capital letters.

Parameters

T: Comparable type of element to be compared.

Example

Output:

How to sort a list in Java


Java Collections.reverseOrder() method

It is the method of Java Collections class which belong to a java.lang package. It returns a comparator that imposes reverse of the natural ordering. The signature of the method is:

Parameters

The class of the objects compared by the comparator.

Example

Output:

How to sort a list in Java


Java Collections.sort() method

The Collections class has two methods for sorting a list:

sort() method

The sort() method sorts the list in ascending order, according to the natural ordering of its elements. The signature of the method is:

Parameters

T: It is a type parameter.

list: The list to be sorted.

Let’s see another example of Collections.sorts() method.

Example

Output:

How to sort a list in Java


Next TopicJava Tutorial

You may also like