Java List sort() MethodThe 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. SyntaxParametersThe parameter 'c' represents the Comparator used to compare list elements. And for null values, natural ordering is used. ReturnNA 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 1Test it NowOutput: List : [Renu, Heera, Vijay, Geetanjali] Sorted List : [Geetanjali, Heera, Renu, Vijay] Example 2Test it NowOutput: 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 |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India