java.util.Collections.sort() method is present in java.util.Collections class. It is used to sort the elements present in the specified list of Collection in ascending order. It works similar to java.util.Arrays.sort() method but it is better than as it can sort the elements of Array as well as linked list, queue and many more present in it.
public static void sort(List myList)
myList : A List type object we want to sort.
This method doesn't return anything
Example:
Let us suppose that our list contains
{"Geeks For Geeks", "Friends", "Dear", "Is", "Superb"}
After using Collection.sort(), we obtain a sorted list as
{"Dear", "Friends", "Geeks For Geeks", "Is", "Superb"}
Sorting an ArrayList in ascending order
JAVA
import java.util.*;
public class Collectionsorting
{
public static void main(String[] args)
{
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(al);
System.out.println( "List after the use of" +
" Collection.sort() :\n" + al);
}
}
|
OutputList 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(1)
Sorting an ArrayList in descending order
JAVA
import java.util.*;
public class Collectionsorting
{
public static void main(String[] args)
{
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(al, Collections.reverseOrder());
System.out.println( "List after the use of" +
" Collection.sort() :\n" + al);
}
}
|
OutputList 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(1)
Sorting an ArrayList according to user defined criteria. We can use Comparator Interface for this purpose.
Java
import java.util.*;
import java.lang.*;
import java.io.*;
class Student
{
int rollno;
String name, address;
public Student( int rollno, String name,
String address)
{
this .rollno = rollno;
this .name = name;
this .address = address;
}
public String toString()
{
return this .rollno + " " + this .name +
" " + this .address;
}
}
class Sortbyroll implements Comparator<Student>
{
public int compare(Student a, Student b)
{
return a.rollno - b.rollno;
}
}
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));
}
}
|
OutputUnsorted
111 bbbb london
131 aaaa nyc
121 cccc jaipur
Sorted by rollno
111 bbbb london
121 cccc jaipur
131 aaaa nyc
Arrays.sort() vs Collections.sort() Arrays.sort works for arrays which can be of primitive data type also. Collections.sort() works for objects Collections like ArrayList, LinkedList, etc. We can use Collections.sort() to sort an array after creating an ArrayList of given array items.
JAVA
import java.util.*;
public class Collectionsort
{
public static void main(String[] args)
{
String domains[] = { "Practice" , "Geeks" ,
"Code" , "Quiz" };
List colList =
new ArrayList(Arrays.asList(domains));
Collections.sort(colList);
System.out.println( "List after the use of" +
" Collection.sort() :\n" +
colList);
}
}
|
OutputList after the use of Collection.sort() :
[Code, Geeks, Practice, Quiz]
Arrays.sort() vs Collections.sort() time complexity :
Arrays.sort() uses a Dual-Pivot Quicksort algorithm which gives a time complexity of O(N.log N) which is typically faster than traditional Quicksort algorithms. On the other hand, Collections.sort() creates an array of list elements, sorts them using an adaptive Mergesort algorithm, and iterates over the list to position each element at its correct location. Thus for primitive datatypes like int, char, double, etc. Arrays.sort() proves to be way more time efficient than Collections.sort(). Problems involving primitive datatypes should be tried to solve using Arrays.sort() for better optimisation.
Below is the code to demonstrate the difference:
Java
import java.io.*;
import java.util.*;
class GFG {
public static void main (String[] args) {
int len = 5000000 ;
int [] arr = new int [len];
for ( int i = len; i > 0 ; i--)
arr[len - i] = i;
ArrayList<Integer> list = new ArrayList<>();
for ( int i = len; i > 0 ; i--)
list.add(i);
long startA = System.currentTimeMillis();
Arrays.sort(arr);
long stopA = System.currentTimeMillis();
long startAL = System.currentTimeMillis();
Collections.sort(list);
long stopAL = System.currentTimeMillis();
System.out.println( "Time taken by Arrays.sort(): " + (stopA - startA));
System.out.println( "Time taken by Collections.sort(): " + (stopAL - startAL));
}
}
|
OutputTime taken by Arrays.sort(): 29
Time taken by Collections.sort(): 42
This article is contributed by Mohit Gupta. The article is wished to be useful to the esteemed Geeks. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. .