How to Sort a List of ArrayList in Java

Avatar

By squashlabs, Last Updated: Nov. 3, 2023

How to Sort a List of ArrayList in Java

To sort a list of ArrayList in Java, you can use the Collections.sort() method or the List.sort() method. Both methods allow you to sort the elements of a list in ascending order, based on their natural order or using a custom comparator. Here are the steps to sort a list in Java:

Using Collections.sort()

1. Import the necessary classes:

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

2. Create an ArrayList and add elements to it:

List list = new ArrayList();
list.add("Apple");
list.add("Orange");
list.add("Banana");

3. Sort the list using the Collections.sort() method:

Collections.sort(list);

4. The list is now sorted in ascending order.

Related Article: Java Printf Method Tutorial

Using List.sort()

1. Import the necessary classes:

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

2. Create an ArrayList and add elements to it:

List list = new ArrayList();
list.add("Apple");
list.add("Orange");
list.add("Banana");

3. Sort the list using the List.sort() method and a lambda expression:

list.sort((a, b) -> a.compareTo(b));

4. The list is now sorted in ascending order.

Sorting in Descending Order

To sort the list in descending order, you can use the Collections.reverseOrder() method as a comparator. Here's how:

1. Import the necessary classes:

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

2. Create an ArrayList and add elements to it:

List list = new ArrayList();
list.add("Apple");
list.add("Orange");
list.add("Banana");

3. Sort the list in descending order using the Collections.sort() method and Collections.reverseOrder() as a comparator:

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

4. The list is now sorted in descending order.

Sorting Custom Objects

If you have a list of custom objects, you can implement the Comparable interface in your object class and override the compareTo() method to specify how the objects should be sorted. Here's an example:

public class Person implements Comparable {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    // Getters and setters

    @Override
    public int compareTo(Person other) {
        return this.age - other.age;
    }
}

Now, you can sort a list of Person objects based on their age:

List list = new ArrayList();
list.add(new Person("John", 25));
list.add(new Person("Alice", 30));
list.add(new Person("Bob", 20));

Collections.sort(list);

// The list is now sorted based on age in ascending order

Note: If you want to sort the list in descending order, you can modify the compareTo() method to return other.age - this.age.

Java Classloader: How to Load Classes in Java

This article Learn how to load classes in Java using the Java Classloader. This article covers the introduction to class loaders, class loader hiera… read more

How to Implement a Strategy Design Pattern in Java

The Strategy Design Pattern is a powerful tool for designing flexible and maintainable Java applications. In this article, we provide a step-by-step … read more

How to Convert a String to an Array in Java

This article provides a simple tutorial on converting a String to an Array in Java. It covers various methods and best practices for string to array … read more

How to Pass Arguments by Value in Java

Java programming involves passing arguments by value, and it is essential to understand how this parameter passing works. This article provides an in… read more

How to Use Hibernate in Java

This article provides an introduction to Hibernate and guides you on how to use it in Java applications. You will learn about essential Hibernate com… read more

How To Iterate Over Entries In A Java Map

Efficiently iterating over entries in a Java map is a common task for software engineers. In this article, you will learn how to iterate over entries… read more

Merge Sort Algorithm in Java and Python

This article provides a practical guide on how to implement the Merge Sort Algorithm in both Java and Python programming languages. The article cover… read more

How to Fix the java.lang.reflect.InvocationTargetException

This article provides insights into the causes and solutions for java.lang.reflect.InvocationTargetException. It covers topics such as inspecting the… read more

How to Use the Java Command Line Arguments

Command line arguments are an essential part of Java programming. This tutorial will teach you how to use command line arguments in your Java applica… read more

Java Exception Handling Tutorial

Exception handling is a critical aspect of Java programming. This tutorial will guide you through the basics of exception handling, including the hie… read more