Sorting user-defined objects is a common requirement in Java applications. When working with a custom class such as a Triplet, Java provides the Comparable and Comparator interfaces to define custom sorting logic based on one or more object attributes.
- Comparable defines the natural ordering of objects within the class itself.
- Comparator allows custom sorting logic to be implemented outside the class.
- Both approaches can be used to sort triplets based on any element (x, y, or z), depending on the application requirements.
Example:
Input: { {1, 2, 3}, {2, 2, 4}, {5, 6, 1}, {10, 2, 10} }
Output: { {5, 6, 1}, {1, 2, 3}, {2, 2, 4}, {10, 2, 10} } // sorting based on third elementsInput: { {10, 20, 30}, {40, -1, 2}, {30, 10, -1}, {50, 10, 50} }
Output: { {30, 18, -1}, {40, -1, 2}, {10, 20, 30}, {50, 10, 50} } //// sorting based on third elements
Methods to Sort an Array of Triplets
Java provides the following two approaches to sort an array of user-defined Triplet objects.
1. Sort an Array of Triplets Using Comparable Interface
The Comparable interface is used when the class itself defines its natural ordering. By implementing the compareTo() method in the Triplet class, objects can be sorted directly using Arrays.sort().
Syntax
class Triplet implements Comparable<Triplet> {
@Override
public int compareTo(Triplet other) {
return this.z - other.z;
}
}
Steps to Sort an Array of Triplets using Comparable Interface:
- Create a Triplet class that implements the Comparable interface.
- Override the compareTo() method to compare the last element (z).
- Create and populate an array of Triplet objects.
- Call Arrays.sort() to sort the array.
- Print the sorted triplets.
import java.util.Arrays;
class Triplet implements Comparable<Triplet> {
int x, y, z;
public Triplet(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
@Override
public int compareTo(Triplet other) {
return Integer.compare(this.z, other.z);
}
@Override
public String toString() {
return "(" + x + ", " + y + ", " + z + ")";
}
}
class GFG {
public static void main(String[] args) {
Triplet[] arr = {
new Triplet(1, 2, 3),
new Triplet(2, 2, 4),
new Triplet(5, 6, 1),
new Triplet(10, 2, 10)
};
// Sort the array based on the last element (z)
Arrays.sort(arr);
// Print the sorted triplets
for (Triplet triplet : arr) {
System.out.println(triplet);
}
}
}
Output
(5, 6, 1) (1, 2, 3) (2, 2, 4) (10, 2, 10)
Explanation: The following example demonstrates how the Comparable interface is used to define the natural ordering of Triplet objects. The compareTo() method compares the last element (z) of each triplet, and Arrays.sort() arranges the array in ascending order based on that value.
2. Sort an Array of Triplets Using Comparator Interface
The Comparator interface is used when the sorting logic needs to be defined separately from the class. It provides greater flexibility because multiple comparison strategies can be implemented without modifying the Triplet class.
Syntax
class Compare implements Comparator<Triplet> {
@Override
public int compare(Triplet a, Triplet b) {
return a.z - b.z;
}
}
Steps to Sort an Array of Triplets using Comparator Interface
- Create a Triplet class.
- Create a separate class that implements the Comparator interface.
- Override the compare() method to compare the last element (z).
- Create and populate an array of Triplet objects.
- Pass the comparator object to Arrays.sort() to sort the array.
- Print the sorted triplets.
import java.util.*;
class Triplet {
int x;
int y;
int z;
public Triplet(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
@Override
public String toString() {
return "(" + x + "," + y + "," + z + ")";
}
}
class Compare implements Comparator<Triplet> {
// Compare triplets based on the last element (z)
@Override
public int compare(Triplet a, Triplet b) {
return Integer.compare(a.z, b.z);
}
}
class GFG {
public static void main(String[] args) {
Triplet[] arr = {
new Triplet(10, 20, 30),
new Triplet(40, -1, 2),
new Triplet(30, 18, -1),
new Triplet(50, 10, 50)
};
// Sorting the array using Comparator
Arrays.sort(arr, new Compare());
// Printing the sorted array
print(arr);
}
public static void print(Triplet[] arr) {
for (Triplet triplet : arr) {
System.out.println(triplet);
}
}
}
Output
(30,18,-1) (40,-1,2) (10,20,30) (50,10,50)
Explanation: The following example demonstrates how a separate Comparator class defines the sorting logic by overriding the compare() method. The comparator compares the last element (z) of each triplet, and Arrays.sort() uses this comparison to arrange the array in ascending order.