
- Java.util - Home
- Java.util - ArrayDeque
- Java.util - ArrayList
- Java.util - Arrays
- Java.util - BitSet
- Java.util - Calendar
- Java.util - Collections
- Java.util - Currency
- Java.util - Date
- Java.util - Dictionary
- Java.util - EnumMap
- Java.util - EnumSet
- Java.util - Formatter
- Java.util - GregorianCalendar
- Java.util - HashMap
- Java.util - HashSet
- Java.util - Hashtable
- Java.util - IdentityHashMap
- Java.util - LinkedHashMap
- Java.util - LinkedHashSet
- Java.util - LinkedList
- Java.util - ListResourceBundle
- Java.util - Locale
- Java.util - Observable
- Java.util - PriorityQueue
- Java.util - Properties
- Java.util - PropertyPermission
- Java.util - PropertyResourceBundle
- Java.util - Random
- Java.util - ResourceBundle
- Java.util - ResourceBundle.Control
- Java.util - Scanner
- Java.util - ServiceLoader
- Java.util - SimpleTimeZone
- Java.util - Stack
- Java.util - StringTokenizer
- Java.util - Timer
- Java.util - TimerTask
- Java.util - TimeZone
- Java.util - TreeMap
- Java.util - TreeSet
- Java.util - UUID
- Java.util - Vector
- Java.util - WeakHashMap
- Java.util - Interfaces
- Java.util - Exceptions
- Java.util - Enumerations
- Java.util Useful Resources
- Java.util - Useful Resources
- Java.util - Discussion
Java Collections sort() Method
Description
The Java Collections sort(List<T>) method is used to sort the specified list into ascending order, according to the natural ordering of its element.
Declaration
Following is the declaration for java.util.Collections.sort() method.
public static <T extends Comparable<? super T>> void sort(List<T> list)
Parameters
list − This is the list to be sorted.
Return Value
NA
Exception
ClassCastException − Throws if the list contains elements that are not mutually comparable (for example, strings and integers).
UnsupportedOperationException − Throws if the specified list's list-iterator does not support the set operation.
Java Collections sort(List, Comparator) Method
Description
The sort(List<T>,Comparator<? super T>) method is used to sort the specified list according to the order induced by the specified comparator.
Declaration
Following is the declaration for java.util.Collections.sort() method.
public static <T> void sort(List<T> list,Comparator<? super T> c)
Parameters
list − This is the list to be sorted.
c − This is the comparator to determine the order of the list.
Return Value
NA
Exception
ClassCastException − Throws if the list contains elements that are not mutually comparable using the specified comparator.
UnsupportedOperationException − Throws if the specified list's list-iterator does not support the set operation.
Sorting List of Integers Example
The following example shows the usage of Java Collection sort(List) method to get a sorted list using natural order. We've created a List object with some integers. Using sort(List) method, we've sorted the list and then it is printed.
package com.tutorialspoint; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; public class CollectionsDemo { public static void main(String[] args) { List<Integer> list = new ArrayList<>(Arrays.asList(1,4,3,6,5,2)); System.out.println("Initial collection value: " + list); // sort the list. Collections.sort(list); System.out.println("Final collection value: " + list); } }
Output
Let us compile and run the above program, this will produce the following result.
Initial collection value: [1, 4, 3, 6, 5, 2] Final collection value: [1, 2, 3, 4, 5, 6]
Sorting List of Comparable Objects Example
The following example shows the usage of Java Collection sort(List) method to get a sorted list using natural order. We've created a List object with some Student objects. Using sort(List) method, we've sorted the list and then it is printed.
package com.tutorialspoint; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; public class CollectionsDemo { public static void main(String[] args) { List<Student> list = new ArrayList<>(Arrays.asList(new Student(1, "Julie"), new Student(3, "Adam"), new Student(2, "Robert"))); System.out.println("Initial collection value: " + list); // sort the list. Collections.sort(list); System.out.println("Final collection value: " + list); } } class Student implements Comparable<Student> { int rollNo; String name; Student(int rollNo, String name){ this.rollNo = rollNo; this.name = name; } @Override public String toString() { return "[ " + this.rollNo + ", " + this.name + " ]"; } @Override public boolean equals(Object obj) { Student s = (Student)obj; return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name); } @Override public int compareTo(Student student) { return this.rollNo - student.rollNo; } }
Output
Let us compile and run the above program, this will produce the following result.
Initial collection value: [[ 1, Julie ], [ 3, Adam ], [ 2, Robert ]] Final collection value: [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ]]
Sorting List of Objects using Comparator Example
The following example shows the usage of Java Collection sort(List, Comparator) method to get a sorted list using given comparator. We've created a List object with some Student objects. Using sort(List) method, we've sorted the list and then it is printed.
package com.tutorialspoint; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Comparator; public class CollectionsDemo { public static void main(String[] args) { List<Student> list = new ArrayList<>(Arrays.asList(new Student(1, "Julie"), new Student(3, "Adam"), new Student(2, "Robert"))); System.out.println("Initial collection value: " + list); RollNoComparator comparator = new RollNoComparator(); // sort the list. Collections.sort(list,comparator); System.out.println("Final collection value: " + list); } } class Student { int rollNo; String name; Student(int rollNo, String name){ this.rollNo = rollNo; this.name = name; } @Override public String toString() { return "[ " + this.rollNo + ", " + this.name + " ]"; } @Override public boolean equals(Object obj) { Student s = (Student)obj; return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name); } public int getRollNo() { return rollNo; } public void setRollNo(int rollNo) { this.rollNo = rollNo; } public String getName() { return name; } public void setName(String name) { this.name = name; } } class RollNoComparator implements Comparator<Student>{ @Override public int compare(Student o1, Student o2) { return o1.getRollNo()-o2.getRollNo(); } }
Output
Let us compile and run the above program, this will produce the following result.
Initial collection value: [[ 1, Julie ], [ 3, Adam ], [ 2, Robert ]] Final collection value: [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ]]