2

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.

Bart
  • 33
  • 4
  • You can find most of what you need [here](https://stackoverflow.com/questions/28823670/how-to-sort-jtable-in-shortest-way). The bottom line is to leverage the JTable's features. – Jeff Holt Apr 10 '18 at 15:46

1 Answers1

0

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 
Justin Albano
  • 3,809
  • 2
  • 24
  • 51
  • Apologies, this was a typo. Even with these changes it doesn't work unfortunately. – Bart Apr 10 '18 at 15:36
  • Be sure that you are comparing `a` and `b`, not `a` and `a`. Even with the edit of your question, the comparison is between `a` and `a`: `int result = Double.compare(a[2], a[2])`. I have updated this answer to include an example of the changed method, along with example output. – Justin Albano Apr 10 '18 at 15:44
  • Thank you for your reply. Unfortunately even with these changes it still provides the same output. Any other thoughts? – Bart Apr 10 '18 at 15:49
  • Cna you update your question to include the code you are using to test this snippet? There may be an issue with how the method is called or other external issues separate from the code. – Justin Albano Apr 10 '18 at 15:51
  • Can you please include your test data, results, and expected results? The code in this answer was tested using the example included, but I want to make sure I'm not misunderstanding your question. – Justin Albano Apr 10 '18 at 16:09
  • This is very difficult due to some external sources I use. However, I did some more research and I believe that the sorting goes right, except that the code does not stick to the current order in 1 row. This means that it sorts ascendingly all of column 2 and then all of column 3, but it does not keep track of which element belongs to which instance. Does this help? – Bart Apr 10 '18 at 16:42
  • Please see https://chat.stackoverflow.com/rooms/168665/nested-sorting-2d-array to further discuss this problem. – Justin Albano Apr 10 '18 at 16:46
  • Unfortunately my reputation at this point is too low to participate in the chat. I need 4 more to be able to participate. I will update you when I have this. – Bart Apr 10 '18 at 16:50
  • I do not fully understand the ordering you are seeing (in your previous comment). Can you please provide an example in the question? – Justin Albano Apr 10 '18 at 16:59
  • I found the answer. Your questions helped me analyse the problem and it turned out that this had to do with the importing of the data. Thank you for your help Justin Albano! – Bart Apr 10 '18 at 17:28
  • @Bart You're welcome. If this or any answer has solved your question please consider [accepting it](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this. – Justin Albano Apr 10 '18 at 20:02