Java Sort Custom Objects in Ascending and Descending Order

1. Introduction

Sorting custom objects in Java is a common task, often achieved using the Comparator interface. With Java 8 and later, this task becomes more straightforward due to lambda expressions, which allow us to define sorting logic in a more concise manner. In this guide, we will explore how to sort custom objects in both ascending and descending order.

2. Program Steps

1. Define a custom Product class with attributes productName and price.

2. Create a list of Product objects.

3. Sort the list by the productName attribute in ascending order.

4. Sort the list by the productName attribute in descending order.

5. Sort the list by the price attribute in ascending order.

6. Sort the list by the price attribute in descending order.

3. Code Program

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

class Product {
    private String productName;
    private double price;

    public Product(String productName, double price) {
        this.productName = productName;
        this.price = price;
    }

    public String getProductName() {
        return productName;
    }

    public double getPrice() {
        return price;
    }

    @Override
    public String toString() {
        return "Product{" + "productName='" + productName + '\'' + ", price=" + price + '}';
    }
}

public class SortCustomObjects {
    public static void main(String[] args) {
        // 2. Create a list of Product objects.
        List<Product> products = new ArrayList<>();
        products.add(new Product("Laptop", 1000.99));
        products.add(new Product("Mobile", 500.49));
        products.add(new Product("Tablet", 300.29));

        // 3. Sort the list by the productName attribute in ascending order.
        products.sort(Comparator.comparing(Product::getProductName));
        System.out.println("Sorted by product name (ascending): " + products);

        // 4. Sort the list by the productName attribute in descending order.
        products.sort(Comparator.comparing(Product::getProductName).reversed());
        System.out.println("Sorted by product name (descending): " + products);

        // 5. Sort the list by the price attribute in ascending order.
        products.sort(Comparator.comparingDouble(Product::getPrice));
        System.out.println("Sorted by price (ascending): " + products);

        // 6. Sort the list by the price attribute in descending order.
        products.sort(Comparator.comparingDouble(Product::getPrice).reversed());
        System.out.println("Sorted by price (descending): " + products);
    }
}

Output:

Sorted by product name (ascending): [Product{productName='Laptop', price=1000.99}, Product{productName='Mobile', price=500.49}, Product{productName='Tablet', price=300.29}]
Sorted by product name (descending): [Product{productName='Tablet', price=300.29}, Product{productName='Mobile', price=500.49}, Product{productName='Laptop', price=1000.99}]
Sorted by price (ascending): [Product{productName='Tablet', price=300.29}, Product{productName='Mobile', price=500.49}, Product{productName='Laptop', price=1000.99}]
Sorted by price (descending): [Product{productName='Laptop', price=1000.99}, Product{productName='Mobile', price=500.49}, Product{productName='Tablet', price=300.29}]

4. Step By Step Explanation

1. We define a Product class with attributes productName and price, along with getter methods and a toString() method for easy display.

2. We create a list of Product objects that we want to sort.

3. To sort by product name in ascending order, we use Comparator.comparing with a method reference to the getProductName method of the Product class.

4. To sort in descending order, the reversed() method is chained after the comparing method to reverse the order.

5. Similarly, for sorting by price, we use Comparator.comparingDouble given that price is a double. We can chain the reversed() method to sort in descending order.

6. The sorted products are printed after each sorting operation to showcase the order.

With the help of lambda expressions and method references, sorting custom objects, such as our Product, is both efficient and concise in Java 8 and beyond.

Comments