Sorting ArrayList using sort() method of List in Java 8

In this article, we will discuss how to sort contents of ArrayList using sort() method of List in Java 8

 

Until Java 1.7 version:

  • To sort ArrayList contents either in ascending/descending order
  • then we have to use Collections class’ utility method sort(); method 
  • by passing arraylist contents

 

From Java 1.8 version onwards:

  • We can sort ArrayList contents using List’s sort(); method

Method signature:

 default void sort(Comparator<? super E> c);

Rules:

  • If the argument to sort() method is null, then objects must be Comparable type like String or Wrapper classes (Integer or Double)
  • Otherwise, we need to define Comparator within the sort method as shown in the example 3
  • Sort method of List takes Comparator as arguments which can be used to sort arraylist contents either in ascending or descending order

 

Sorting ArrayList using sort() method of List in Java 8 :

  • To sort ArrayList of Integer values
  • To sort ArrayList of String values
  • To sort ArrayList of Custom objects

 

Example 1: To sort ArrayList of Integer values

  • This program is used to sort ArrayList
  • consisting of Integer values in ascending order

IntegerArrayListSortingInJava8.java

package in.bench.resources.arraylist.sorting.in.java8;

import java.util.ArrayList;
import java.util.List;

public class IntegerArrayListSortingInJava8 {

	public static void main(String[] args) {

		// creating ArrayList object of type Integer
		List<Integer> numbers = new ArrayList<Integer>();

		// adding elements to ArrayList object
		numbers.add(369);
		numbers.add(358);
		numbers.add(364);
		numbers.add(370);
		numbers.add(397);
		numbers.add(311);
		numbers.add(386);

		// Iterating using enhanced for-loop
		System.out.println("Before Sorting : Insertion Order\n");
		for(Integer number : numbers) {
			System.out.println(number);
		}


		// to sort ArrayList contents in ascending order
		numbers.sort(null);

		// ascending order of ArrayList contents
		System.out.println("\n\n\nAfter Sorting : Ascending order\n");
		for(Integer number : numbers) {
			System.out.println(number);
		}
	}
}

Output:

Before Sorting : Insertion Order

369
358
364
370
397
311
386



After Sorting : Ascending order

311
358
364
369
370
386
397

 

Example 2: To sort ArrayList of String values

  • This program is used to sort ArrayList
  • consisting of String values in ascending order

StringArrayListSortingInJava8.java

package in.bench.resources.arraylist.sorting.in.java8;

import java.util.ArrayList;
import java.util.List;

public class StringArrayListSortingInJava8 {

	public static void main(String[] args) {

		// creating ArrayList object of type String
		List<String> companies = new ArrayList<String>();

		// adding elements to ArrayList object
		companies.add("LinkedIn");
		companies.add("Amazon");
		companies.add("Google");
		companies.add("Apple");
		companies.add("Facebook");
		companies.add("Oracle");
		companies.add("Microsoft");

		// Iterating using enhanced for-loop
		System.out.println("Before Sorting : Insertion Order\n");
		for(String company : companies) {
			System.out.println(company);
		}


		// to sort ArrayList contents in ascending order
		companies.sort(null);

		// ascending order of ArrayList contents
		System.out.println("\n\n\nAfter Sorting : Ascending order\n");
		for(String company : companies) {
			System.out.println(company);
		}
	}
}

Output:

Before Sorting : Insertion Order

LinkedIn
Amazon
Google
Apple
Facebook
Oracle
Microsoft



After Sorting : Ascending order

Amazon
Apple
Facebook
Google
LinkedIn
Microsoft
Oracle

 

Example 3: To sort ArrayList of Custom object like Student

  • POJO class for Student

Student.java

package in.bench.resources.arraylist.sorting.in.java8;

public class Student {

	// member variables
	int studRoll;
	String studName;

	// public 2-arg parameterized constructor
	public Student(int studRoll, String studName) {
		this.studRoll = studRoll;
		this.studName = studName;
	}

	// getter & setter
	public int getStudRoll() {
		return studRoll;
	}
	public void setStudRoll(int studRoll) {
		this.studRoll = studRoll;
	}
	public String getStudName() {
		return studName;
	}
	public void setStudName(String studName) {
		this.studName = studName;
	}

	// toString method
	@Override
	public String toString() {
		return "Student ["
				+ "studRoll=" + studRoll 
				+ ", studName=" + studName 
				+ "]";
	}
}

Main class :

  • Below program is used to sort ArrayList contents of Student object
  • both in ascending as well as descending order

StudentArrayListSortingInJava8.java

package in.bench.resources.arraylist.sorting.in.java8;

import java.util.ArrayList;
import java.util.List;

public class StudentArrayListSortingInJava8 {

	public static void main(String[] args) {

		// creating sample Student Object
		Student stud1 = new Student(368, "Genie");
		Student stud2 = new Student(355, "Tumba");
		Student stud3 = new Student(368, "Devhur");
		Student stud4 = new Student(311, "Aanik");
		Student stud5 = new Student(386, "Sultan");

		// creating ArrayList object of type Student
		List<Student> students = new ArrayList<Student>();

		// adding Student Object to ArrayList
		students.add(stud1);
		students.add(stud2);
		students.add(stud3);
		students.add(stud4);
		students.add(stud5);


		// Iterating using enhanced for-loop
		System.out.println("Before Sorting : Insertion Order\n");
		for(Student stud : students) {
			System.out.println(stud);
		}


		// to sort ArrayList contents in ascending order
		students.sort((s1, s2) -> s1.getStudName()
				.compareTo(s2.getStudName()));

		// ascending order of ArrayList contents
		System.out.println("\n\n\nAscending order"
				+ " Sorting acc to Student name\n");
		for(Student stud : students) {
			System.out.println(stud);
		}


		// to sort ArrayList contents in descending order
		students.sort((s1, s2) -> s2.getStudName()
				.compareTo(s1.getStudName()));

		// descending order of ArrayList contents
		System.out.println("\n\n\nDescending order"
				+ " Sorting acc to Student name\n");
		for(Student stud : students) {
			System.out.println(stud);
		}
	}
}

Output:

Before Sorting : Insertion Order

Student [studRoll=368, studName=Genie]
Student [studRoll=355, studName=Tumba]
Student [studRoll=368, studName=Devhur]
Student [studRoll=311, studName=Aanik]
Student [studRoll=386, studName=Sultan]



Ascending order Sorting acc to Student name

Student [studRoll=311, studName=Aanik]
Student [studRoll=368, studName=Devhur]
Student [studRoll=368, studName=Genie]
Student [studRoll=386, studName=Sultan]
Student [studRoll=355, studName=Tumba]



Descending order Sorting acc to Student name

Student [studRoll=355, studName=Tumba]
Student [studRoll=386, studName=Sultan]
Student [studRoll=368, studName=Genie]
Student [studRoll=368, studName=Devhur]
Student [studRoll=311, studName=Aanik]

 

Hope, you found this article very helpful. If you any suggestion or want to contribute any other way or tricky situation you faced during Interview hours, then share with us. We will include that code here.

 

References:

 

Happy Coding !!
Happy Learning !!

Various ways to iterate Arrays in Java – 5 ways
JDBC: An example to connect MS Access database in Java 8