![]() |
VOOZH | about |
The Collections.sort() method in Java is used to sort elements of a List in ascending order. It is part of the java.util.Collections class and works with List implementations like ArrayList and LinkedList. It simplifies sorting by providing a built-in mechanism.
Collections.sort(List<T> list);
Example: Sorting an ArrayList in ascending order
List after the use of Collection.sort() : [Dear, Friends, Geeks For Geeks, Is, Superb]
Example: Sorting an ArrayList in descending order
List after the use of Collection.sort() : [Superb, Is, Geeks For Geeks, Friends, Dear]
Sorting an ArrayList according to user defined criteria. We can use Comparator Interface for this purpose.
Custom Sorting using Comparato
Unsorted 111 bbbb london 131 aaaa nyc 121 cccc jaipur Sorted by rollno 111 bbbb london 121 cccc jaipur 131 aaaa nyc