简体   繁体   中英

Nested sorting 2d array java

I have a 2D array with 8 columns (col0-col7) and a thousand rows that I would like to sort ascending on 2 columns: col2 and col3. I would like to give priority to column col2 and first sort on that column. If two values in col2 are equal (very high chance) then I would like to sort based on col3. This is the current code I use:

public static void sortMyArray() {
    Arrays.sort(myArray, new Comparator<double[]>() {
        public int compare(double[] a, double[] b) {
            int result = Double.compare(a[2], b[2]); 
            if (result == 0) {
                return Double.compare(a[3], b[3]);
            } else {
                return result;
            }
        }
    });
}

The current code I use sorts my array only on col3. I would like to stick to an 2d array due to later calculations.

Hopefully you can help me.

EDIT: made a few changes based on feedback from a user (for which many thanks!). However, the problem remains. This is the code I used to test the snippet:

   public static void loadTotalData() {
    for (int i = 0; i < myArray.length; i++) {
        myArray[i][0] = classOriginal.allData[i].col0data;
        myArray[i][1] = classOriginal.allData[i].col1data;
        myArray[i][2] = classOriginal.allData[i].col2data;
        myArray[i][3] = classOriginal.allData[i].col3data;
        myArray[i][4] = 0.0;
        myArray[i][5] = 0.0;
        myArray[i][6] = 0.0;
        myArray[i][7] = 0.0;
    }

    System.out.println(myArray[0][3]);  
    System.out.println(myArray[1][3]);
    System.out.println(myArray[2][3]);
    System.out.println(myArray[3][3]);
    System.out.println(myArray[4][3]);
    System.out.println(myArray[5][3]);
    System.out.println(myArray[6][3]);
    System.out.println(myArray[7][3]);
    System.out.println(myArray[8][3]);
    System.out.println(myArray[9][3]);
    System.out.println(myArray[10][3]);

    classForSortingAndLoading.sortMyArray();

    System.out.println(myArray[0][3]);  
    System.out.println(myArray[1][3]);
    System.out.println(myArray[2][3]);
    System.out.println(myArray[3][3]);
    System.out.println(myArray[4][3]);
    System.out.println(myArray[5][3]);
    System.out.println(myArray[6][3]);
    System.out.println(myArray[7][3]);
    System.out.println(myArray[8][3]);
    System.out.println(myArray[9][3]);
    System.out.println(myArray[10][3]);

Probably not the most efficient way to test this, but I know the data source and thus I know what it should give back before and after the sorting.

You should be comparing columns of a and b , rather than a and a :

public static void sortMyArray() {
    Arrays.sort(myArray, new Comparator<double[]>() {
        public int compare(double[] a, double[] b) {
            int result = Double.compare(a[2], b[2]); 
            if (result == 0) {
                return Double.compare(a[3], b[3]);
            } else {
                return result;
            }
        }
    });
}

Since a[2] equals a[2] by definition (identity), result was always 0 and thus, only column 3 was ever compared, which again would always return 0 ( return results branch of the conditional was never executed).


Example:

public class Main {

    public static void main(String[] args) {
        double[][] myArray = sortMyArray();
        printArray(myArray);
    }

    public static double[][] sortMyArray() {
        double[][] myArray = new double[][] {
            {1, 11, 21, 37, 41, 51, 61},
            {0, 10, 20, 30, 40, 50, 60},
            {2, 12, 22, 32, 42, 52, 62},
            {1, 11, 21, 31, 41, 51, 61},
        };
        Arrays.sort(myArray, new Comparator<double[]>() {
            public int compare(double[] a, double[] b) {
                int result = Double.compare(a[2], b[2]); 
                if (result == 0) {
                    return Double.compare(a[3], b[3]);
                } else {
                    return result;
                }
            }
        });

        return myArray;
    }

    private static void printArray(double[][] myArray) {
        for(int row = 0; row < myArray.length; row++){
            for( int column = 0; column < myArray[0].length; column++){
                System.out.print(myArray[row][column] + " ");
            }
            System.out.println();
        }
    }
}

Executing this code results in the following output:

0.0 10.0 20.0 30.0 40.0 50.0 60.0 
1.0 11.0 21.0 31.0 41.0 51.0 61.0 
1.0 11.0 21.0 37.0 41.0 51.0 61.0 
2.0 12.0 22.0 32.0 42.0 52.0 62.0 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM