0

I have a 2 dimensional array, that has dimensions of myArray[x][3]. I need to sort the array based upon [x][0]. I was using Arrays.sort(myArray);. That was working, however, the array at the time was a one dimension array of myArray[x]. Then I changed my mind and changed it into a 2 dimensional array. It is filled with integers from 1 to 9. I have searched for the clear method to sort 2 dimensional arrays, and cannot find the simple explanations. Please help.

Thanks; Ice

Ok, here is the code:

public static void sortArray(int myArray[][]){
Arrays.sort(myArray, new Comparator<Integer[]>(){
    @Override
    public int compare(Integer[] o1, Integer[] o2) {
        return o1[0].compareTo(o2[0]);
    }
});

Did that work?

OK, here is the problem. The sorted array starts out unsorted, like this:

3 - 0 - 0
4 - 0 - 1
5 - 0 - 2
6 - 0 - 3
3 - 0 - 4

The first column [0][x] is the value, the second column [1][x] is the array field count, and the last column [2][x] is the actual column number in the array. The overall method, takes a entire row from the original 2 dimensional array, and loads it into a 3-tall by x-wide array, then sorts the array based on the [0][x] column. Here is the result after the sort function now being called:

0 - 0 - 3
0 - 1 - 4
0 - 2 - 5
0 - 3 - 6
0 - 4 - 3

Somehow, the method I copied and pasted, is swapping out the numbers, seems like the sorting is wrong. Same System.out.print is being used on both outputs.

César
  • 9,939
  • 6
  • 53
  • 74
IceRegent
  • 17
  • 1
  • 1
  • 7
  • Do you want the entire 2-D array sorted as a whole, or do you want each row of the array sorted individually? – wkl Oct 26 '11 at 20:14
  • You mean that you want to sort the array column wise ? – rohit89 Oct 26 '11 at 20:16
  • I need the array to be sorted based upon column 0, keeping the columns bound together. The first column contains the value, the second column contains basically a x co-ordinate, the third column contains basically the y value, so, yes, they need to stay together. The first column is what needs to be sorted, from smallest to largest. – IceRegent Oct 26 '11 at 20:19
  • You can adapt the answer from this question: http://stackoverflow.com/questions/4907683/sort-a-two-dimensional-array-based-on-one-column into your data type. It sorts on the first column. – wkl Oct 26 '11 at 20:21
  • ok, I will go look at that and see how it can be adapted. Thanks – IceRegent Oct 26 '11 at 20:22
  • not that easy for me to understand how to adapt that...:( – IceRegent Oct 26 '11 at 20:27

2 Answers2

3

If I got it right:

    Integer[][] numbers = new Integer[][]{{7, 8, 9}, {1, 2, 3}};
    System.out.println("Before:");
    for(Integer[] row : numbers) {
        for(Integer num : row) {
            System.out.print(num);
        }
        System.out.println("");
    }

    Arrays.sort(numbers, new Comparator<Integer[]>(){
        @Override
        public int compare(Integer[] o1, Integer[] o2) {
            return o1[0].compareTo(o2[0]);
        }
    });
    System.out.println("After:");
    for(Integer[] row : numbers) {
        for(Integer num : row) {
            System.out.print(num);
        }
        System.out.println("");
    }

Prints:

Before:
789
123
After:
123
789

Update:

This exactly what you need.

public static void sortArray(int myArray[][]) {
    Arrays.sort(myArray, new Comparator<int[]>() {

        @Override
        public int compare(int[] o1, int[] o2) {
            return Integer.valueOf(o1[0]).compareTo(Integer.valueOf(o2[0]));
        }

    });
}

Update2:

Sorting each row:

public static void sortEachRow(int myArray[][]) {
    for(int[] row : myArray) {
        Arrays.sort(row);
    }
}
Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
  • Here is what I have placed into a method, so that i can call it to make it work...`code`public static void sortArray(int myArray[][]){ Arrays.sort(numbers, new Comparator(){ @Override public int compare(Integer[] o1, Integer[] o2) { return o1[0].compareTo(o2[0]); } }); }`code` – IceRegent Oct 26 '11 at 20:37
  • @IceRegent: Update your post with method. Also, you forgot tell what problem you are having now. – Bhesh Gurung Oct 26 '11 at 20:40
  • I apologize, I am not finding how to update the post with method. The problem is, I am getting errors when I place that code into my method in my netbeans. the "Arrays.sort(numbers, new Comparator(){", the word "numbers" is underlined, but, obviously, I replace it with my array name, myArray, and then the entire line of code gets underlined. – IceRegent Oct 26 '11 at 20:44
  • When I click the link edit, it just allows me to add or change the current comment, and I tried surrounding my code with the `code` keys, but it doesnt change anything. – IceRegent Oct 26 '11 at 20:45
  • @IceRegent: Not that __edit__, the __edit__ at the bottom part of your original post. It's right under, where you have written: "Thanks; Ice". – Bhesh Gurung Oct 26 '11 at 20:47
  • @IceRegent: See update. I added the method you need, you can copy it and it will work. Don't forget to accept the answer if it worked. – Bhesh Gurung Oct 26 '11 at 21:00
  • lol. You are too fast for me. I just saw your update, and copied and pasted it. Now I go back to my code, and my passing of the array to the method is not creating any errors. Sorry I replied before I saw your update. My entire code so far, is over 207 lines thus far, so I do not want to post the entire friggin code here, to bugger up the pages, lol. But now, I need to understand it, and verify that it is working, and the problem is, my second array is [3][x], whereas the first array was [x][3], so I need to sort on rows in one, and on columns in the other. – IceRegent Oct 26 '11 at 21:24
  • I need to keep the original x and y values associated with the data. I have an original array, with [x][x] size. I copy out a row at a time into another array, copying both the data in the cell, and the row and column number. I then need to sort the new array, keeping the row and column number associated with the data cell. – IceRegent Oct 26 '11 at 21:58
0

This should work.

public static void main(final String[] args) 
{
    Integer[][] numbers = new Integer[][] {{7, 8, 9}, {1, 2, 3}};
    sortArray(numbers);
    for (Integer[] s : numbers) {
        System.out.println(s[0] + " " + s[1] + " " + s[2]);
    }
}

public static void sortArray(Integer myArray[][])
{ 
    Arrays.sort(myArray, new Comparator<Integer[]>()
    { 
            @Override 
            public int compare(Integer[] o1, Integer[] o2) 
            {
                    return o1[0].compareTo(o2[0]); 
            } 
    }); 
}
rohit89
  • 5,745
  • 2
  • 25
  • 42
  • Seems to give no errors, but when i try to pass my array from inside my other code, it underlines in red as in an error condition: sortArray(myArray); – IceRegent Oct 26 '11 at 21:03
  • Not sure I understand. The parameter should be `numbers` not `myArray`. Or did you mean that `sortArray(numbers)` gives an error ? – rohit89 Oct 26 '11 at 21:10
  • No, in the first part, you are creating an array and filling it, then displaying it with your name of the array. I already have an array created, actually i am working with 3 different arrays, but i just need to sort one of them right now. The name of the array I need to sort, is called myRows. So, when I call the method sortArray, I need to pass the actual array of "myRows". – IceRegent Oct 26 '11 at 21:18
  • Are the arrays declared as `Integer` not `int`? The declaration for `sortArray` also has `Integer` as parameter. – rohit89 Oct 26 '11 at 21:28
  • the arrays are declared as int. – IceRegent Oct 26 '11 at 21:35
  • Declare it as `Integer`. It should work now. The errors are because of mismatch between `int` and `Integer`. – rohit89 Oct 26 '11 at 21:39