Introduction to Sorting in Collection
Sorting in Collections can be done using the method Collections.sort(), which is instantiated from the class java.util.Collections. It can be done on the basis of natural ordering or the order provided by the programmer. For that, the Custom comparator has to be used. Even though sorting in the collection is similar to the method java.util.Arrays.sort(), it sorts not only the array elements but the queue, linked list elements, etc.
Methods of Sorting in Collection
The collection.sort() methods for two different types of sorting are:
- sort(List li): Elements of the list li will be sorted based on the natural ordering. That is, ascending order.
- sort(List li, Comparator c): Elements of the list li will be sorted based on comparator c.
How Sorting works in Collection?
Sorting in the collection can be done in different manners. Moreover, elements of objects such as string objects, wrapper class objects, user-defined class objects can also be sorted.
Let us see how sorting can be done in different ways.
1. Sort(List li)
This method helps in sorting the elements based on natural ordering.
Suppose there is a list li, and there are some elements such as 1,4,5,6,9,3,0.
ArrayList<String> li = new ArrayList<String>();
li.add("1");
li.add("4");
li.add("5");
li.add("6");
li.add("9");
li.add("3");
li.add("0");
If we are trying to sort this, the output will be in ascending order, as shown below.
[0, 1, 3, 4, 5, 6, 9]Even though we didn’t mention anything explicitly, natural ordering is done based on the ascending order.
2. sort(List li, Comparator c)
In some cases, the list can’t be sorted based on natural ordering. At that time, the custom comparator will be used.
Suppose there is a list li and there are some students and their marks as elements.
List<Student1> li = new ArrayList<Student1>();
//add elements to the list li
li.add(new Student1("Anna",40));
li.add(new Student1("Alan",48));
li.add(new Student1("Adu",49));
li.add(new Student1("Iza",50));
If we are trying to sort this based on their mark, a custom comparator has to be used as shown below:
Collections.sort(li,new Marks());
Here, Marks class is the comparator, and code has to be implemented for that.
//class to compare
class Marks implements Comparator<Student1>{
}
Examples of Sorting in Collection
Given below are the examples of Sorting in Collection:
Example #1
Program to sort the elements in a list based on the natural ordering.
Code:
import java.util.ArrayList;
import java.util.Collections;
import java.util.SortedSet;
import java.util.TreeSet;
//class
public class SortCollectionExample {
//main method
public static void main(String[] args) {
// list creation
ArrayList<String> li = new ArrayList<String>();
// Adding elements to a list
li.add("1");
li.add("4");
li.add("5");
li.add("6");
li.add("9");
li.add("3");
li.add("0");
//print the list
System.out.println("Befor sorting, List is: " + li);
// sort the list
Collections.sort(li);
System.out.println("The sorted list is: " + li);
}
}
Output:
Here, a list is created first, and elements are added to it. After adding the elements, the list is printed in order to identify how the list will look before sorting. Once it is printed, the sort() method is called, and the sorted method will be printed.
Example #2
Program to sort the elements in a list based on the custom comparator.
Code:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class SortComparator {
public static void main(String a[]){
//create a list li for students
List<Student1> li = new ArrayList<Student1>();
//add elements to the list li
li.add(new Student1("Anna",40));
li.add(new Student1("Alan",48));
li.add(new Student1("Adu",49));
li.add(new Student1("Iza",50));
//sort the elements using the comparator Marks
Collections.sort(li,new Marks());
//sorted elements of the list
System.out.println("Sorted list elements are: ");
//print each student details by using for loop
for(Student1 s:li){
System.out.println(s);
}
}
}
//class to compare
class Marks implements Comparator<Student1>{
//compare student details
@Override
public int compare(Student1 s1, Student1 s2) {
//if the mark of student s1,return 1
if(s1.getMark() < s2.getMark()){
return 1;
} else {
return -1;
}
}
}
//student details
class Student1{
//declare variables for name and mark
private String name;
private int mark;
//constructor of class
public Student1(String nm, int mr){
this.name = nm;
this.mark = mr;
}
//get the name
public String getName() {
// return the name
return name;
}
//set the nam
public void setName(String nm) {
//set nm as name
this.name = nm;
}
//get the mark
public int getMark() {
return mark;
}
//set the mark
public void setMark(int mark) {
this.mark = mark;
}
public String toString(){
return "Name is : "+this.name+" and the mark is: "+this.mark;
}
}
Output:
In this program also, a list is created first, and elements are added to it. After adding the elements, the sort() method is called, which contains a custom comparator Mark. In that class, the marks of the students are compared to the class Student. Student class contains the details of students such as Name and Mark. After comparison, the list will be printed based on the descending order of marks.
Example #3
Program to sort the elements in a list in reverse order.
Code:
import java.util.ArrayList;
import java.util.Collections;
import java.util.SortedSet;
import java.util.TreeSet;
//class
public class ReverseSort {
//main method
public static void main(String[] args) {
// list creation
ArrayList<String> li = new ArrayList<String>();
// Adding elements to a list
li.add("1");
li.add("4");
li.add("5");
li.add("6");
li.add("9");
li.add("3");
li.add("0");
//print the list
System.out.println("Befor sorting, List is: " + li);
// sort the list
Collections.sort(li, Collections.reverseOrder());
System.out.println("The sorted list is: " + li);
}
}
Output:
Here, similar to the first program, a list is created first, and elements are added to it. After adding the elements, the list is printed. Once it is printed, the sort() method is called. The difference is that, here, the sorting will be in reverse order. Then, the sorted method will be printed.
Conclusion
In Java, sorting in Collections is done using the method Collections.sort(). It can be done based on the natural ordering or Custom comparator, based on the user’s requirement. In this document, a detailed explanation of the sorting of the collection is explained.
Recommended Articles
This is a guide to Sorting in Collection. Here we discuss the introduction to Sorting in Collection along with the methods, working and examples. You may also have a look at the following articles to learn more –