Java 如何对二维数组进行排序?

Java 如何对二维数组进行排序?,java,arrays,sorting,Java,Arrays,Sorting,我需要按递增顺序对二维数组进行排序 例如: 之前: 5.1 3.3 6.3 4.8 4.9 6.9 7.4 5.2 3.6 7.4 之后: 3.6 3.3 4.9 4.8 5.1 5.2 6.3 6.9 7.4 7.4 您可以使用以下方式进行尝试

我需要按递增顺序对二维数组进行排序

例如:

之前:

5.1            3.3

6.3            4.8

4.9            6.9

7.4            5.2

3.6            7.4
之后:

3.6            3.3

4.9            4.8

5.1            5.2   

6.3            6.9   

7.4            7.4 

您可以使用以下方式进行尝试:

java.util.Arrays.sort(数组,新的java.util.Comparator(){
公共整数比较(双[]a,双[]b){
返回Double.compare(a[0],b[0]);
}

您可以使用以下方法进行尝试:

java.util.Arrays.sort(数组,新的java.util.Comparator(){
公共整数比较(双[]a,双[]b){
返回Double.compare(a[0],b[0]);
}

我认为首先要将2D数组转换为1D,然后再对数组进行排序
再次将其转换为一维到二维,另一种方法是对数组进行排序
List ar=new ArrayList();
对于(inti=0;i
我认为首先要将2D数组转换为1D,然后再对数组进行排序
再次将其转换为一维到二维,另一种方法是对数组进行排序
List ar=new ArrayList();

对于(int i=0;iIs有什么您已经尝试过的吗?有什么您已经尝试过的吗?这不会使两行数字保持在一起吗?从示例输入来看,似乎每一列都独立排序。我知道了。它对每一列进行了排序。这不会使两行数字保持在一起吗?从示例输入来看,似乎每一列都在一起排序独立地。我知道了。它对每一列进行了排序。
java.util.Arrays.sort(array, new java.util.Comparator<double[]>() {
    public int compare(double[] a, double[] b) {
        return Double.compare(a[0], b[0]);
    }
double[][] array = {
                    {5.1, 3.3}, 
                    {6.3, 4.8}, 
                    {4.9, 6.9}, 
                    {7.4, 5.2}, 
                    {3.6, 7.4}, 
                   };
// for the number of columns in each row (2D so 0 and 1)
for (int col = 0; col <= 1; col++) {

    // create a temporary array to store this column in the 2D array
    double[] thisColumn = new double[array.length];

    // asign the temporary column values
    for (int row = 0; row < array.length; row++) {
        thisColumn[row] = array[row][col];
    }

    // sort that column
    Arrays.sort(thisColumn);

    // reassign it back to the original array
    for (int row = 0; row < array.length; row++) {
         array[row][col] = thisColumn[row];
    }
}
// print it out
for (double[] r : array) {
    for (double c : r) {
        System.out.print(c + "\t");
    }
    System.out.println();
}
3.6    3.3  
4.9    4.8  
5.1    5.2  
6.3    6.9  
7.4    7.4  
I think first you convert your 2D array into 1D than sorted your array after that
again convert it into 1D to 2D and another way is to sort you array

 List<Double> ar = new ArrayList<>();
    for(int i=0;i<array.length;i++){
        for(int j=0;j<array[i].length;j++){
            ar.add(a[i][j]);
        }
    }
    Collections.sort(ar);
    for (Double double1 : ar) {
        System.out.println(double1);
    }
}