Sorting:
Sorting is a process of arranging data in an increasing or decreasing order.
The elements of the String objects, the Wrapper class objects, and the User-defined class objects can be sorted. For sorting the elements of a collection, static methods are provided by the Collections class.
As we discussed earlier that TreeSet and TreeMap maintain order for their elements. But list not provides any facility for ordering the elements. Collections classes provide the sort() method to arrange list elements.
Note: By using the sort() method we can sort the String and Wrapper class objects only but not user-defined objects. It is because String and Wrapper classes implement a Comparable interface.
Method of Collections class for sorting List elements:
public void sort(List list):
Uses: To sort the elements of List, where the elements of the List must be of the Comparable type.
Example of sorting the elements of List that contain string and Wrapper class objects:
SortingTest.java
import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; /** * This class is used to show the sorting functionality. * @author w3schools */ public class SortingTest { public static void main(String args[]){ ArrayList arrayList1 = new ArrayList(); ArrayList arrayList2 = new ArrayList(); //Add string objects to the ArrayList. arrayList1.add("Amani"); arrayList1.add("Prabhjot"); arrayList1.add("Nidhi"); arrayList1.add("Vandana"); arrayList1.add("Poonam"); //Add Wrapper objects to the ArrayList. arrayList2.add(Integer.valueOf(12)); arrayList2.add(Integer.valueOf(34)); arrayList2.add(Integer.valueOf(14)); arrayList2.add(Integer.valueOf(56)); arrayList2.add(Integer.valueOf(4)); //Print the Collection string elements before sorting. Iterator iterator1=arrayList1.iterator(); System.out.println("Collection string elements " + "before sorting:"); while(iterator1.hasNext()){ System.out.println(iterator1.next()); } //Call the Collections sort method for sorting Collections.sort(arrayList1); //Print the Collection string elements before sorting. Iterator iterator2=arrayList1.iterator(); System.out.println("Collection string elements " + "after sorting:"); while(iterator2.hasNext()){ System.out.println(iterator2.next()); } //Print the Collection Wrapper elements before sorting. Iterator iterator3=arrayList2.iterator(); System.out.println("Collection Wrapper elements " + "before sorting:"); while(iterator3.hasNext()){ System.out.println(iterator3.next()); } //Call the Collections sort method for sorting Collections.sort(arrayList2); //Print the Collection Wrapper elements before sorting. Iterator iterator4=arrayList2.iterator(); System.out.println("Collection Wrapper elements " + "after sorting:"); while(iterator4.hasNext()){ System.out.println(iterator4.next()); } } } |
Output
Collection string elements before sorting: Amani Prabhjot Nidhi Vandana Poonam Collection string elements after sorting: Amani Nidhi Poonam Prabhjot Vandana Collection Wrapper elements before sorting: 12 34 14 56 4 Collection Wrapper elements after sorting: 4 12 14 34 56 |
Example: To sort string objects in reverse order:
import java.util.*; public class TestSortExample{ public static void main(String args[]){ ArrayList al=new ArrayList(); al.add("Z"); al.add("B"); al.add("S"); al.add("D"); Collections.sort(al,Collections.reverseOrder()); Iterator i=al.iterator(); while(i.hasNext()) { System.out.println(i.next()); } } } |
Output
Z S D B |
Please see below articles to sort user-defined class objects:
Next Topic: Comparable interface in java with example.
Previous Topic: Abstract classes in collection framework in java with example.