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.

6
  • Do you want the entire 2-D array sorted as a whole, or do you want each row of the array sorted individually? Commented Oct 26, 2011 at 20:14
  • You mean that you want to sort the array column wise ? Commented Oct 26, 2011 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. Commented Oct 26, 2011 at 20:19
  • You can adapt the answer from this question: stackoverflow.com/questions/4907683/… into your data type. It sorts on the first column. Commented Oct 26, 2011 at 20:21
  • ok, I will go look at that and see how it can be adapted. Thanks Commented Oct 26, 2011 at 20:22

2 Answers 2

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);
    }
}
Sign up to request clarification or add additional context in comments.

8 Comments

Here is what I have placed into a method, so that i can call it to make it work...codepublic static void sortArray(int myArray[][]){ Arrays.sort(numbers, new Comparator<Integer[]>(){ @Override public int compare(Integer[] o1, Integer[] o2) { return o1[0].compareTo(o2[0]); } }); }code
@IceRegent: Update your post with method. Also, you forgot tell what problem you are having now.
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<Integer[]>(){", the word "numbers" is underlined, but, obviously, I replace it with my array name, myArray, and then the entire line of code gets underlined.
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: Not that edit, the edit at the bottom part of your original post. It's right under, where you have written: "Thanks; Ice".
|
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]); 
            } 
    }); 
}

6 Comments

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);
Not sure I understand. The parameter should be numbers not myArray. Or did you mean that sortArray(numbers) gives an error ?
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".
Are the arrays declared as Integer not int? The declaration for sortArray also has Integer as parameter.
the arrays are declared as int.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.