How to Sort a List of ArrayList in Java

Avatar

By squashlabs, Last Updated: November 2, 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: How to Convert List to Array in Java

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.

Related Article: Tutorial: Sorted Data Structure Storage in Java

Tutorial: Enumeration Types and Data Structures in Java

Enumeration types in Java provide a powerful way to define a set of named values. In this article, we will explore how enumeration types can be used to hold data... read more

How to Initialize an ArrayList in One Line in Java

This article provides a simple guide on initializing an ArrayList in one line using Java. It covers different approaches like using the Arrays.asList() method and Java... read more

How to Easily Print a Java Array

Printing a Java Array can be done simply using either a loop or the Arrays class. This article provides a concise guide on the best practices for printing a Java... read more

How to Declare and Initialize a New Array in Java

Declaring and initializing arrays in Java is a fundamental skill for any programmer. This article provides a step-by-step guide to help you understand and implement this... read more

How to Print an ArrayList in Java

Printing an ArrayList in Java can be done in multiple ways. One approach is to use a for-each loop, which allows you to iterate through the elements of the ArrayList and... read more

Saving Tree Data Structure in Java File: A Discussion

This article provides an in-depth exploration of methods for storing tree data structures in Java files. It covers topics such as file handling, serialization, utilizing... read more