Custom Sort List in Java

1. Introduction

Sorting a list in ascending and descending order is a fundamental operation in Java, allowing for data organisation for further processing or presentation. Custom sorting criteria can be particularly useful when working with complex data types or specific sorting requirements. Java's Collections. sort() method, along with a custom Comparator, provides a flexible way to sort lists according to custom-defined rules. This blog post will illustrate how to sort a list of custom objects in Java first in ascending order and then in descending order.

2. Program Steps

1. Define a class to represent the objects in the list.

2. Create and populate a list with instances of the class.

3. Define two custom Comparators for ascending and descending order sorting.

4. Use the Collections.sort() method with the custom Comparator to sort the list in ascending order.

5. Repeat the sorting using the Comparator for descending order.

6. Display the sorted list after each sort to illustrate the order.

3. Code Program

import java.util.*;

// Step 1: Defining the class
class Fruit {
    String name;
    int quantity;

    Fruit(String name, int quantity) {
        this.name = name;
        this.quantity = quantity;
    }

    @Override
    public String toString() {
        return name + ": " + quantity;
    }
}

public class CustomSortListOrder {
    public static void main(String[] args) {
        // Step 2: Creating and populating the list
        List<Fruit> fruits = new ArrayList<>();
        fruits.add(new Fruit("Apple", 50));
        fruits.add(new Fruit("Orange", 20));
        fruits.add(new Fruit("Banana", 40));

        // Step 3: Defining custom Comparators
        Comparator<Fruit> sortByQuantityAscending = Comparator.comparingInt(f -> f.quantity);
        Comparator<Fruit> sortByQuantityDescending = (f1, f2) -> f2.quantity - f1.quantity;

        // Step 4: Sorting in ascending order
        Collections.sort(fruits, sortByQuantityAscending);
        System.out.println("Fruits sorted by quantity (ascending):");
        fruits.forEach(System.out::println);

        // Step 5: Sorting in descending order
        Collections.sort(fruits, sortByQuantityDescending);
        System.out.println("\nFruits sorted by quantity (descending):");
        fruits.forEach(System.out::println);
    }
}

Output:

Fruits sorted by quantity (ascending):
Orange: 20
Banana: 40
Apple: 50
Fruits sorted by quantity (descending):
Apple: 50
Banana: 40
Orange: 20

Explanation:

1. The Fruit class is defined with name and quantity fields, a constructor, and a toString method for display purposes.

2. A List<Fruit> named fruits is created and populated with Fruit objects.

3. Two Comparator<Fruit> objects are defined for sorting: sortByQuantityAscending uses the Comparator.comparingInt method for natural integer sorting (ascending), while sortByQuantityDescending implements a lambda expression to sort integers in reverse (descending).

4. The list is first sorted in ascending order using Collections.sort(fruits, sortByQuantityAscending), and the sorted list is printed to demonstrate the ascending order sorting.

5. The list is then sorted in descending order using Collections.sort(fruits, sortByQuantityDescending), and the sorted list is printed to show the descending order.

6. This approach illustrates how to use custom Comparators to sort a list of objects in Java according to ascending and descending order, highlighting the versatility and power of Java's Collections Framework.

Comments