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
import java.util.*;
public class Collectionsorting
{
public static void main(String[] args)
{
// Create a list of strings
ArrayList<String> al = new ArrayList<String>();
al.add("Geeks For Geeks");
al.add("Friends");
al.add("Dear");
al.add("Is");
al.add("Superb");
/* Collections.sort method is sorting the
elements of ArrayList in ascending order. */
Collections.sort(al);
// Let us print the sorted list
System.out.println("List after the use of" +
" Collection.sort() :\n" + al);
}
}
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
import java.util.*;
public class Collectionsorting
{
public static void main(String[] args)
{
// Create a list of strings
ArrayList<String> al = new ArrayList<String>();
al.add("Geeks For Geeks");
al.add("Friends");
al.add("Dear");
al.add("Is");
al.add("Superb");
/* Collections.sort method is sorting the
elements of ArrayList in ascending order. */
Collections.sort(al, Collections.reverseOrder());
// Let us print the sorted list
System.out.println("List after the use of" +
" Collection.sort() :\n" + al);
}
}
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
import java.util.*;
import java.lang.*;
import java.io.*;
// A class to represent a student.
class Student
{
int rollno;
String name, address;
// Constructor
public Student(int rollno, String name,
String address)
{
this.rollno = rollno;
this.name = name;
this.address = address;
}
// Used to print student details in main()
public String toString()
{
return this.rollno + " " + this.name +
" " + this.address;
}
}
class Sortbyroll implements Comparator<Student>
{
// Used for sorting in ascending order of
// roll number
public int compare(Student a, Student b)
{
return a.rollno - b.rollno;
}
}
// Driver class
class Main
{
public static void main (String[] args)
{
ArrayList<Student> ar = new ArrayList<Student>();
ar.add(new Student(111, "bbbb", "london"));
ar.add(new Student(131, "aaaa", "nyc"));
ar.add(new Student(121, "cccc", "jaipur"));
System.out.println("Unsorted");
for (int i=0; i<ar.size(); i++)
System.out.println(ar.get(i));
Collections.sort(ar, new Sortbyroll());
System.out.println("\nSorted by rollno");
for (int i=0; i<ar.size(); i++)
System.out.println(ar.get(i));
}
}
Output
Unsorted 111 bbbb london 131 aaaa nyc 121 cccc jaipur Sorted by rollno 111 bbbb london 121 cccc jaipur 131 aaaa nyc
