Home » Java List sort() Method with Examples

Java List sort() Method with Examples

by Online Tutorials Library

Java List sort() Method

The sort() method of List Interface sorts the given list according to the order specified in the comparator. The list must be modifiable else it will throw an exception.

Syntax

Parameters

The parameter ‘c’ represents the Comparator used to compare list elements. And for null values, natural ordering is used.

Return

NA

Throws:

ClassCastException- If the list contains elements that are not mutually comparable using the comparator

UnsupportedOperationException– If the list’s list-iterator does not support the sort operation

IllegalArgumentException– If the comparator is found to violate the Comparator protocols.

Example 1

Test it Now

Output:

List : [Renu, Heera, Vijay, Geetanjali]  Sorted List : [Geetanjali, Heera, Renu, Vijay]  

Example 2

Test it Now

Output:

Unsorted List :  1. 15019 Patanjali  2. 13198 Geetanjali  3. 12112 Anjali     Sorted List :  1. 12112 Anjali  2. 13198 Geetanjali  3. 15019 Patanjali  
Next TopicJava List

You may also like