Open In App

Sort an Array of Triplet using Java Comparable and Comparator

Last Updated : 26 Nov, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

Given an array of integer Triplet. you have to sort the array in ascending order with respect to the last element in the triplet.

Examples:

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} } Input: { {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} }

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Method 1: Using Comparable Interface

  • In this method, we are going to implement the Comparable interface from java.lang Package in the Triplet class.
  • The Comparable interface contains the method compareTo to decide the order of the elements.
  • Override the compareTo method in the Triplet class.
  • Create an array of Triplet and populate the array.
  • Use the Arrays.sort() function to sort the array.

Java




importjava.io.*;
importjava.util.*;
 
classTriplet implementsComparable<Triplet> {
    intx;
    inty;
    intz;
   
    publicTriplet(intx,inty,intz){
        this.x = x;
        this.y = y;
        this.z = z;
    }
 
    publicString toString() {
        return"("+ x + ","+ y + ","+ z + ")";
    }
     
      // Overridden method to compare
      // values of the last element.
    publicintcompareTo(Triplet a){
        returnthis.z - a.z;
    }
}
 
classGFG {
    publicstaticvoidmain (String[] args) {    
       
        intn = 4;
        Triplet arr[] = newTriplet[n];
 
        arr[0] = newTriplet(1, 2, 3);
        arr[1] = newTriplet(2, 2, 4);
        arr[2] = newTriplet(5, 6, 1);
        arr[3] = newTriplet(10, 2, 10);
  
          // Sorting the array
        Arrays.sort(arr);   
           
          // printing the
          // Triplet array
          print(arr);           
    }
   
      publicstaticvoidprint(Triplet[] arr){
        for(inti = 0;i < arr.length;i++){
            System.out.println(arr[i]);
        }
    }
}

Output:

(5,6,1) (1,2,3) (2,2,4) (10,2,10)

Method 2: Using Comparator Interface

  • In this method, we create a separate Compare class that implements Comparator Interface
  • The Comparable interface contains the method compared to order the elements.
  • Override the compare method in the Compare class.
  • Create an array of Triplet and populate the array.
  • Use the Arrays.sort() function to sort the array and pass an object of Compare class.

Java




importjava.io.*;
importjava.util.*;
 
classTriplet {
    intx;
    inty;
    intz;
    publicTriplet(intx,inty,intz){
        this.x = x;
        this.y = y;
        this.z = z;
    }
      publicString toString() {
        return"("+ x + ","+ y + ","+ z + ")";
    }
}
 
classCompare implementsComparator<Triplet>{
 
    // Overridden compare method to
      // compare objects for sorting.
    publicintcompare(Triplet a,Triplet b){
        returna.z - b.z;
    }
}
 
classGFG {
    publicstaticvoidmain (String[] args) {    
       
        intn = 4;
        Triplet arr[] = newTriplet[n];
 
        arr[0] = newTriplet(10, 20, 30);
        arr[1] = newTriplet(40, -1, 2);
        arr[2] = newTriplet(30, 18, -1);
        arr[3] = newTriplet(50, 10, 50);
  
          // Sorting the array by passing
          // Compare object
        Arrays.sort(arr, newCompare());   
       
        // printing the Triplet array
          print(arr);           
    }
   
      publicstaticvoidprint(Triplet[] arr){
        for(inti = 0;i < arr.length;i++){
            System.out.println(arr[i]);
        }
    }
}

Output:

(30,18,-1) (40,-1,2) (10,20,30) (50,10,50)

In this article, we sorted a user-defined triplet by using java comparable and comparator interface. Remember, the same can be implemented for any element in the triplet just by the change the variable name in the overridden class methods in the compareTo and compare.



Next Article

Similar Reads

close