CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > How to > How to use Comparator with Array in Java?

How to use Comparator with Array in Java?

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Last updated: July 1, 2026 ยท 1 min read ยท 0 Comments
Share: in X

In the following example, we’ve defined two custom comparators: AscendingComparator and DescendingComparator. These comparators implement the Comparator interface and override the compare method to provide the desired comparison logic.

We then use these comparators to sort an array of integers in both ascending and descending order using the Arrays.sort() method. The output will show the array sorted first in ascending order and then in descending order.

Now let’s see the practical example below:

import java.util.Arrays;
import java.util.Comparator;

public class ArrayComparatorExample {
    public static void main(String[] args) {
        // Sample array of integers
        Integer[] numbers = { 5, 2, 8, 1, 3 };

        // Using a Comparator to sort the array in ascending order
        Arrays.sort(numbers, new AscendingComparator());

        System.out.println("Array sorted in ascending order: " + Arrays.toString(numbers));

        // Using a Comparator to sort the array in descending order
        Arrays.sort(numbers, new DescendingComparator());

        System.out.println("Array sorted in descending order: " + Arrays.toString(numbers));
    }
}

// Comparator for ascending order
class AscendingComparator implements Comparator<Integer> {
    @Override
    public int compare(Integer o1, Integer o2) {
        return o1.compareTo(o2);
    }
}

// Comparator for descending order
class DescendingComparator implements Comparator<Integer> {
    @Override
    public int compare(Integer o1, Integer o2) {
        return o2.compareTo(o1);
    }
}

Output:

Array sorted in ascending order: [1, 2, 3, 5, 8]
Array sorted in descending order: [8, 5, 3, 2, 1]

Related Posts:

  • Comparator in Java
  • Arrays in Java
  • Pagination and Sorting in Spring Boot
  • User Defined Exception in Java
  • Integers In Java
  • Arrays of Primitive Types in Java
Tags:collection-frameworkjava
Was this article helpful?
โ† Previous ArticleHow To Print Git Commit Message In Jenkins Pipeline
Next Article โ†’Software Engineering Introduction

Recent Posts

  • How to implement Passwordless Authentication in Spring Boot: A Step-by-Step Guide
  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Linkedin

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
ยฉ 2026 CoderSathi. All rights reserved. Privacy Policy ยท Sitemap