VOOZH about

URL: https://www.geeksforgeeks.org/java/collections-sort-java-examples/

⇱ Collections.sort() in Java with Examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Collections.sort() in Java with Examples

Last Updated : 30 Apr, 2026

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.

  • Sorts elements based on natural ordering or custom Comparator
  • Works with List implementations, not arrays
  • Modifies the list in-place without creating a new list

Syntax

Collections.sort(List<T> list);

  • Sorts the list in ascending order (natural order)
  • Elements must implement Comparable


Example: Sorting an ArrayList in ascending order


Output
List after the use of Collection.sort() :
[Dear, Friends, Geeks For Geeks, Is, Superb]
  • Time Complexity: O(N log N) as time complexity of Collections.sort() is O(nlog(n)).
  • Auxiliary Space: O(N)  

Example: Sorting an ArrayList in descending order 


Output
List after the use of Collection.sort() :
[Superb, Is, Geeks For Geeks, Friends, Dear]
  • Time Complexity: O(N log N) as time complexity of Collections.sort() is O(nlog(n)).
  • Auxiliary Space: O(N)

Custom Sorting

Sorting an ArrayList according to user defined criteria. We can use Comparator Interface for this purpose.

Syntax

Custom Sorting using Comparato

  • Sorts the list using a custom sorting logic
  • We define the order using a Comparator

Output
Unsorted
111 bbbb london
131 aaaa nyc
121 cccc jaipur

Sorted by rollno
111 bbbb london
121 cccc jaipur
131 aaaa nyc
Comment
Article Tags:
Article Tags: