📜  使用Java Comparable 和 Comparator 对 Triplet 数组进行排序

📅  最后修改于: 2022-05-13 01:54:19.381000             🧑  作者: Mango

使用Java Comparable 和 Comparator 对 Triplet 数组进行排序

给定一个整数 Triplet 数组。您必须相对于三元组中的最后一个元素按升序对数组进行排序。

例子:

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} }

推荐:请先在 {IDE} 上尝试您的方法,然后再继续解决。

方法一:使用可比接口

  • 在这个方法中,我们将在 Triplet 类中实现Java.lang包中的Comparable接口。
  • Comparable 接口包含方法compareTo来决定元素的顺序。
  • 覆盖 Triplet 类中的 compareTo 方法。
  • 创建一个 Triplet 数组并填充该数组。
  • 使用Arrays.sort()函数对数组进行排序。
Java
import java.io.*;
import java.util.*;
 
class Triplet implements Comparable {
    int x;
    int y;
    int z;
   
    public Triplet(int x,int y,int z){
        this.x = x;
        this.y = y;
        this.z = z;
    }
 
    public String toString() {
        return "(" + x + "," + y + "," + z + ")";
    }
     
      // Overriden method to compare
      // values of the last element.
    public int compareTo(Triplet a){
        return this.z - a.z;
    }
}
 
class GFG {
    public static void main (String[] args) {    
       
        int n = 4;
        Triplet arr[] = new Triplet[n];
 
        arr[0] = new Triplet(1, 2, 3);
        arr[1] = new Triplet(2, 2, 4);
        arr[2] = new Triplet(5, 6, 1);
        arr[3] = new Triplet(10, 2, 10);
  
          // Sorting the array
        Arrays.sort(arr);   
           
          // printing the
          // Triplet array
          print(arr);           
    }
   
      public static void print(Triplet[] arr){
        for(int i = 0;i < arr.length;i++){
            System.out.println(arr[i]);
        }
    }
}


Java
import java.io.*;
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;
    }
      public String toString() {
        return "(" + x + "," + y + "," + z + ")";
    }
}
 
class Compare implements Comparator{
 
    // Overriden compare method to
      // compare objects for sorting.
    public int compare(Triplet a,Triplet b){
        return a.z - b.z;
    }
}
 
class GFG {
    public static void main (String[] args) {    
       
        int n = 4;
        Triplet arr[] = new Triplet[n];
 
        arr[0] = new Triplet(10, 20, 30);
        arr[1] = new Triplet(40, -1, 2);
        arr[2] = new Triplet(30, 18, -1);
        arr[3] = new Triplet(50, 10, 50);
  
          // Sorting the array by passing
          // Compare object
        Arrays.sort(arr, new Compare());   
       
        // printing the Triplet array
          print(arr);           
    }
   
      public static void print(Triplet[] arr){
        for(int i = 0;i < arr.length;i++){
            System.out.println(arr[i]);
        }
    }
}


输出:

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

方法二:使用比较器接口

  • 在这个方法中,我们创建了一个单独的实现 Comparator 接口的 Compare 类
  • Comparable 接口包含用于对元素进行排序的方法。
  • 重写 Compare 类中的 compare 方法。
  • 创建一个 Triplet 数组并填充该数组。
  • 使用Arrays.sort()函数对数组进行排序并传递一个比较类的对象。

Java

import java.io.*;
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;
    }
      public String toString() {
        return "(" + x + "," + y + "," + z + ")";
    }
}
 
class Compare implements Comparator{
 
    // Overriden compare method to
      // compare objects for sorting.
    public int compare(Triplet a,Triplet b){
        return a.z - b.z;
    }
}
 
class GFG {
    public static void main (String[] args) {    
       
        int n = 4;
        Triplet arr[] = new Triplet[n];
 
        arr[0] = new Triplet(10, 20, 30);
        arr[1] = new Triplet(40, -1, 2);
        arr[2] = new Triplet(30, 18, -1);
        arr[3] = new Triplet(50, 10, 50);
  
          // Sorting the array by passing
          // Compare object
        Arrays.sort(arr, new Compare());   
       
        // printing the Triplet array
          print(arr);           
    }
   
      public static void print(Triplet[] arr){
        for(int i = 0;i < arr.length;i++){
            System.out.println(arr[i]);
        }
    }
}

输出:

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

在本文中,我们使用Java可比较和比较器接口对用户定义的三元组进行排序。请记住,只需更改compareTocompare中重写的类方法中的变量名,就可以对三元组中的任何元素实现相同的操作。