Sorting ArrayList of Objects in Java

In this topic, we will learn how to sort ArrayList of objects in Java. ArrayList is a predefined class provided by the Collection framework and imported from java.util package. This class is commonly used by Java developers in their day-to-day life. There are different ways to sort ArrayList of objects in Java:
    • Using Collections.sort() method
    • Using Java 8 ArrayList of sort() method 
    • Using Java 8 sorted() method

Using Collections.sort() method

We can sort ArrayList of objects in Java with the help of Collections class sort() methods. Collections class gives the two methods to perform sorting the objects of the ArrayList class. 

 • public static void sort(List l): This is used to sort the List elements according to the default natural sorting order. In this the elements of the List should be homogeneous and comparable otherwise we will get ClassCastException. List class should not contain null otherwise we will get NullPointerException.
 • public static void sort(List I, Comparator c): This method is used to sort the List elements according to customized sorting order.

Example of sorting objects of ArrayList according to default natural sorting order

In this example, we can sort ArrayList of objects in ascending order.

import java.util.ArrayList;
import java.util.Collections;
public class SpringJava{
public static void main(String args[]){
//creating an ArrayList
ArrayList al=new ArrayList();
al.add("A");
al.add("Q");
al.add("Z");
al.add("K");
//al.add(new Integer(10));//ClassCastException
//al.add(null);//NullPointerException
System.out.println("Before Sorting: " + al);
//sorting ArrayList in ascending order
Collections.sort(al);
System.out.println("After Sorting: " + al);
    }
}

Output

Before Sorting: [A, Q, Z, K]
After Sorting: [A, K, Q, Z]

Example of sorting objects of ArrayList according to customized sorting order

In this example, we can sort ArrayList of objects in descending order.

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class SpringJava{
public static void main(String args[]){
//creating an ArrayList 
ArrayList al=new ArrayList();
al.add("A");
al.add("Q");
al.add("Z");
al.add("K");
al.add(new Integer(10));
//al.add(null);//NullPointerException
System.out.println("Before Sorting: " + al);
//sorting ArrayList in descending order
Collections.sort(al, new MyComparator());
System.out.println("After Sorting: " + al);
    }
}
class MyComparator implements Comparator{
public int compare(Object obj1, Object obj2){
String s1=obj1.toString();
String s2=obj2.toString();
return s2.compareTo(s1);
 }
}

Output

Before Sorting: [A, Q, Z, K, 10]
After Sorting: [Z, Q, K, A, 10]

Using Java 8  ArrayList of sort() method  

From Java 8 onwards we can use the ArrayList of sort() method to sort ArrayList of objects in ascending and descending order.

public void sort(Comparator c)

Example of sorting objects of ArrayList according to ascending sorting order

import java.util.ArrayList;
import java.util.List;
import java.util.Comparator;
public class SpringJava{
public static void main(String args[]){
//creating an ArrayList
ArrayList<String> al=new ArrayList<>();
al.add("A");
al.add("Q");
al.add("Z");
al.add("K");
System.out.println("Before Sorting: " + al);
//sorting an ArrayList in ascending order
al.sort(Comparator.naturalOrder());
System.out.println("After Sorting: " + al);
    }
}

Output

Before Sorting: [A, Q, Z, K]
After Sorting: [A, K, Q, Z]

Example of sorting objects of ArrayList according to descending sorting order

import java.util.ArrayList;
import java.util.List;
import java.util.Comparator;
public class SpringJava{
public static void main(String args[]){
//creating an ArrayList
ArrayList<String> al=new ArrayList<>();
al.add("A");
al.add("Q");
al.add("Z");
al.add("K");
System.out.println("Before Sorting: " + al);
//sorting an ArrayList in descending order
al.sort(Comparator.reverseOrder());
System.out.println("After Sorting: " + al);
    }
}

Output

Before Sorting: [A, Q, Z, K]
After Sorting: [Z, Q, K, A]

Using Java 8 sorted() method

From Java 8 onward we can use the Stream API sorted() method to sort ArrayList of objects in ascending and descending order. In the Collection interface stream() method added in Java 8 onward. This stream() method returns the Stream interface.

default Stream<E> stream()

Example of sorting ArrayList of Integer type using Stream API sorted() method

import java.util.ArrayList;
import java.util.Comparator;
import java.util.stream.Collectors;
public class SpringJava{
public static void main(String[] args) {
//creating ArrayList
ArrayList<Integer> al = new ArrayList<>();
al.add(10);
al.add(60);
al.add(40);
al.add(50);
al.add(70);
System.out.println("Before Sorting:"+al);
//sorting an ArrayList in ascending
ArrayList<Integer> sortedList = al
                .stream()
                .sorted()
                .collect(Collectors.toCollection(ArrayList::new));
//printing ArrayList
System.out.println("Sorting in Ascending order:"+sortedList);
//sorting an ArrayList in ascending
ArrayList<Integer> reverseSortedList = al.stream()
                .sorted(Comparator.reverseOrder())
                .collect(Collectors.toCollection(ArrayList::new));
//printing ArrayList
System.out.println("Sorting in Descending order:"+reverseSortedList);
    }
}

Output

Before Sorting:[10, 60, 40, 50, 70]
Sorting in Ascending order:[10, 40, 50, 60, 70]
Sorting in Descending order:[70, 60, 50, 40, 10]

Sorting ArrayList of multi fields by using Stream API

import java.util.ArrayList;
import java.util.Comparator;
import java.util.stream.Collectors;
class Person{
private int id;
private String fName;
private String lName;
Person(int id, String fName, String lName){
this.id=id;
this.fName=fName;
this.lName=lName;
}
    public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getFName() {
    return fName;
}
public void setFName(String fName) {
    this.fName = fName;
}
public String getLName() {
    return lName;
}
public void setLName(String lName) {
    this.lName = lName;
  }
}
public class SpringJava{
public static void main(String[] args) {
//creating ArrayList
ArrayList<Person> al = new ArrayList<>();
al.add(new Person(1,"Peter","Parker"));
al.add(new Person(2,"Robert","John"));
al.add(new Person(3,"Bill","Smith"));
System.out.println("----Before Sorting----");
for(Person p:al){
    System.out.println("Person:"+p.getId()+","+p.getFName()+","+p.getLName());
}
//comaparing first name and then last name
Comparator<Person> compareByName = Comparator
                        .comparing(Person::getFName)
                        .thenComparing(Person::getLName);
     
ArrayList<Person> sortedPerson = al.stream()
                    .sorted(compareByName)
                    .collect(Collectors.toCollection(ArrayList::new));
System.out.println("----After Sorting----");
for(Person p:sortedPerson){
    System.out.println("Person:"+p.getId()+","+p.getFName()+","+p.getLName());
     }
   }
}

Output

----Before Sorting----
Person:1,Peter,Parker
Person:2,Robert,John
Person:3,Bill,Smith
----After Sorting----
Person:3,Bill,Smith
Person:1,Peter,Parker
Person:2,Robert,John

FAQ

Q1: What is the simplest way to sort data in an ArrayList of integers?
Ans. By using the sort() method of the Collections class in Java we can sort data in an ArrayList of integers in a simpler way.
Q2: Which class is used to sort ArrayList elements?
Ans. The Collections class is used to sort ArrayList elements.
Q3: How do you sort an ArrayList of objects in descending order?
Ans. By using the sort() method of the Collections class in Java we can sort an ArrayList of objects in descending order.

Conclusion

In this topic, we learned how to sort an ArrayList of objects in Java in various ways.

Leave a Comment