Sort an array of pairs using Java Arrays.sort() with custom Comparator Last Updated : 29 Oct, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Given an array of pairs of integers. The task is to sort the array with respect to the second element of the pair.Example:Input: [(10, 20), (20, 30), (5, 6), (2, 5)] Output: [(2, 5), (5, 6), (10, 20), (20, 30)]Program of Java Array Sort Custom Comparator Java // Java code to sort the array // according to second element import java.util.Arrays; import java.util.Comparator; // User defined Pair class class Pair { int x; int y; public Pair(int x, int y) { this.x = x; this.y = y; } // Overriding toString method // for beautiful printing of pairs @Override public String toString() { return "(" + x + ", " + y + ')'; } } // Class to define user defined comparator class ArrayOfPairsSorter { static void sort(Pair[] a) { Comparator<Pair> comparator = new Comparator<>() { @Override public int compare(Pair p1, Pair p2) { return p1.y - p2.y; // To compare the first element // just change the variable from p1.y // - p2.y to p1.x-p2.x. } }; Arrays.sort(a, comparator); } } class GFG { public static void main(String[] args) { Pair[] a = new Pair[4]; a[0] = new Pair(10, 20); a[1] = new Pair(20, 30); a[2] = new Pair(5, 6); a[3] = new Pair(2, 5); ArrayOfPairsSorter.sort(a); System.out.println(Arrays.toString(a)); } } Output[(2, 5), (5, 6), (10, 20), (20, 30)] Explanation of the above Program: Store the pairs in an array using a user-defined Pair class.Override the comparator method to sort the array according to the second element.Sort the array according to the second element. Comment More infoAdvertise with us Next Article How to Sort an Arrays of Object using Arrays.sort() B barykrg Follow Improve Article Tags : Java Java-Arrays Java-Array-Programs Practice Tags : Java Similar Reads Sort an Array in Java using Comparator A Comparator is an object that can be used to compare two objects and determine their order. We can use a Comparator to sort a list of objects in any order we can choose, not just in ascending order.Examples:Array(Ascending Order): Input: arr = (4, 2, 5, 1, 3) Output: [1, 2, 3, 4, 5] Array(Descendin 4 min read How to Sort an Arrays of Object using Arrays.sort() To sort the array of objects in both ascending and descending order in Java, we can use the Arrays.sort() method with the Comparator. In this article, we will learn how to sort an array of objects using Arrays.sort(). Sorting an Array of ObjectWith Array of Object what it really means is an array st 3 min read How to sort an Array of Strings in Java Array Of StringsTo sort an array of strings in Java, we can use Arrays.sort() function. Java // A sample Java program to // sort an array of strings // in ascending and descending // orders using Arrays.sort(). import java.util.Arrays; import java.util.Collections; // Driver Class public class SortE 3 min read Stream sorted (Comparator comparator) method in Java Stream sorted(Comparator comparator) returns a stream consisting of the elements of this stream, sorted according to the provided Comparator. For ordered streams, the sort method is stable but for unordered streams, no stability is guaranteed. It is a stateful intermediate operation i.e, it may inco 3 min read Java Program to Sort an ArrayList ArrayList is the class provided in the Collection framework. In Java, the collection framework is defined in java.util package. ArrayList is used to dynamically stores the elements. It is more flexible than an array because there is no size limit in ArrayList. ArrayList stores the data in an unorder 6 min read Java Arrays compare() Method with Examples The Arrays compare() method in Java is a part of the java.util package to compare arrays lexicographically (dictionary order). This method is useful for ordering arrays and different overloads for different types including boolean, byte, char, double, float, int, long, short, and Object arrays. Exam 3 min read Like