Sorting Elements of a Java ArrayList Tutorial


In this tutorial, we are going to discuss different approaches to sorting a Java ArrayList.


How to sort Java ArrayList instance ?

Java ArrayList is an ordered collection of objects. By default, Java ArrayList will follow the insertion order to order its objects. However, we can sort the objects in the ArrayList as per our preference by following one of the below approaches.


Sorting an ArrayList using Collections.sort

    List fruits = Arrays.asList("Orange", "Apple", "Grapes", "Mango");

    System.out.println("Ascending order");

    Collections.sort(fruits);   //Ascending order

    fruits.forEach(fruit -> System.out.print(fruit + " "));

    System.out.println();
    System.out.println("Reverse order");

    Collections.sort(fruits, Collections.reverseOrder());   //Reverse order

    fruits.forEach(fruit -> System.out.print(fruit + " "));

Output

Ascending order
Apple Grapes Mango Orange
Reverse order
Orange Mango Grapes Apple

Sorting an ArrayList using a custom Comparator

    List fruits = Arrays.asList("Orange", "Apple", "Grapes", "Mango");

    System.out.println("Ascending order");

    fruits.sort(new Comparator() {
        @Override
        public int compare(String fruit1, String fruit2) {
            return fruit1.compareTo(fruit2);
        }
    });

    fruits.forEach(fruit -> System.out.print(fruit + " "));

    System.out.println();
    System.out.println("Reverse order");

    fruits.sort(new Comparator() {
        @Override
        public int compare(String fruit1, String fruit2) {
            return fruit2.compareTo(fruit1);
        }
    });

    fruits.forEach(fruit -> System.out.print(fruit + " "));

Output

Ascending order
Apple Grapes Mango Orange
Reverse order
Orange Mango Grapes Apple

Sorting an ArrayList by implimenting Comparable interface

    public class Student implements Comparable {
        private Integer studentId;
        private String name;

        public Student(Integer studentId, String name) {
            this.studentId = studentId;
            this.name = name;
        }

        public Integer getStudentId() {
            return studentId;
        }

        public void setStudentId(Integer studentId) {
            this.studentId = studentId;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        @Override
        public int compareTo(Student o) {
            return this.getStudentId().compareTo(o.getStudentId());
        }
    }

    public class Main {
        public static void main(String[] args) {

            List students = new ArrayList<>();
            students.add(new Student(4, "John"));
            students.add(new Student(1, "Henry"));
            students.add(new Student(3, "Mark"));
            students.add(new Student(2, "Bob"));

            System.out.println("Ascending order");

            Collections.sort(students);

            students.forEach(student -> System.out.print(student.getName() + " "));

            System.out.println();
            System.out.println("Reverse order");

            Collections.sort(students, Collections.reverseOrder());

            students.forEach(student -> System.out.print(student.getName() + " "));
        }
    }

Output: Here object list is ordered by student id, not the name

Ascending order
Henry Bob Mark John
Reverse order
John Mark Bob Henry

Sorting a Java ArrayList with Java 8lambda expression

    List fruits = Arrays.asList("Orange", "Apple", "Grapes", "Mango");

    System.out.println("Ascending order");

    fruits.sort((fruit1, fruit2) -> fruit1.compareTo(fruit2));

    fruits.forEach(fruit -> System.out.print(fruit + " "));

    System.out.println();
    System.out.println("Reverse order");

    fruits.sort((fruit1, fruit2) -> fruit2.compareTo(fruit1));

    fruits.forEach(fruit -> System.out.print(fruit + " "));

Output: Here object list is ordered by student id, not the name

Ascending order
    Apple Grapes Mango Orange
    Reverse order
    Orange Mango Grapes Apple

Sorting a Java ArrayList with Java 8method reference

    List fruits = Arrays.asList("Orange", "Apple", "Grapes", "Mango");

    System.out.println("Ascending order");

    fruits.sort(String::compareTo);

    fruits.forEach(fruit -> System.out.print(fruit + " "));

    System.out.println();
    System.out.println("Reverse order");

    fruits.sort(Comparator.comparing(String::toString).reversed());

    fruits.forEach(fruit -> System.out.print(fruit + " "));

Output: Here object list is ordered by student id, not the name

Ascending order
    Apple Grapes Mango Orange
    Reverse order
    Orange Mango Grapes Apple

Sorting a Java ArrayList in Java 8 by multiple level sorting

List students = ....

students.sort(Comparator.comparing(Student::getName).thenComparing(Student::getStudentId));

Sorting a Java ArrayList with Java 8 streem

    List fruits = Arrays.asList("Orange", "Apple", "Grapes", "Mango");

    System.out.println("Ascending order");

    fruits.stream().sorted().collect(Collectors.toList());

    fruits.forEach(fruit -> System.out.print(fruit + " "));

    System.out.println();
    System.out.println("Reverse order");

    fruits.stream().sorted(Collections.reverseOrder()).collect(Collectors.toList());

    fruits.forEach(fruit -> System.out.print(fruit + " "));
    Ascending order
    Apple Grapes Mango Orange
    Reverse order
    Orange Mango Grapes Apple

Sorting a Java ArrayList with Java 8 streem

    List fruits = Arrays.asList("Orange", "Apple", "Grapes", "Mango");

    System.out.println("Ascending order");

    fruits.stream().sorted().collect(Collectors.toList());

    fruits.forEach(fruit -> System.out.print(fruit + " "));

    System.out.println();
    System.out.println("Reverse order");

    fruits.stream().sorted(Collections.reverseOrder()).collect(Collectors.toList());

    fruits.forEach(fruit -> System.out.print(fruit + " "));
    Ascending order
    Apple Grapes Mango Orange
    Reverse order
    Orange Mango Grapes Apple

<< Java ArrayList