Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a 2D-array that I want to sort based on the second column. The first column should remain paired with the second column.

The 2D-array is initially as follows (2x10 matrix):

0 10
1 9
2 9
3 9
4 15
5 10
6 4
7 8
8 11
9 12

I want the above 2D-array to be sorted like this:

4 15
9 12
8 11
0 10
5 10
1 9
2 9
3 9
7 8
6 4

Now, I've tried adapting the answer from: Sort a two dimensional array based on one column into this code:

Arrays.sort(theArray, new Comparator<Integer[]>()
{
    @Override
    public int compare(Integer[] int1, Integer[] int2)
    {
        Integer numOfKeys1 = int1[1];
        Integer numOfKeys2 = int2[1];
        return numOfKeys1.compareTo(numOfKeys2);
    }
});

However, it doesn't seem to sort the array at all. When printing the array after calling the sort() function the array is in its initial order.

I also tried adapting the answer from here: sorting 2D array of String in java but I encountered the same problem.

Have I made some fatal mistake when adapting these solutions, or should my code work?

Also, how would I go about sorting this array in descending order? Would I replace the return statement in compare() with this line?

return -numOfKeys2.compareTo(numOfKeys1);

Any help would be greatly appreciated. Thanks!

EDIT: Just posting the rest of my code to see if the problem is elsewhere.

public void Sort()
{   
    Integer[][] theArray = {{0,10},{1,9},{2,9},{3,9},{4,15},{5,10},{6,4},{7,8},{8,11},{9,12}};;

    dump(theArray);
    Arrays.sort(theArray, new Comparator<Integer[]>()
    {
        @Override
        public int compare(Integer[] int1, Integer[] int2)
        {
            Integer numOfKeys1 = int1[1];
            Integer numOfKeys2 = int2[1];
            return numOfKeys1.compareTo(numOfKeys2);
        }
    });

    System.out.println("====");
    dump(theArray);     
}

public void dump(Integer[][] array)
{
    for(int p = 0, q = 10; p < q; p++)
    {
        System.out.println(array[p][0] + " " + array[p][1]);
    }
}

EDIT 2:

I've got it working. Thanks everyone for your help. I had multiple Sort() functions (an older one that wasn't working, and the one you see above), and it turns out I was calling the wrong one, even though I thought I changed the call. Just one of those days.

Feel free to use the code above if you want to sort an array. It's fully working now.

share|improve this question
    
That code shouldn't run; the array of arrays has a single array with a bunch of two-element arrays in it, you're treating it as a an array of two ten-element arrays when you print it. –  Dave Newton Oct 29 '11 at 7:09
    
You're right, I was getting my indices mixed up. Thanks for correcting me. And thanks for your help. –  Drake Oct 29 '11 at 7:53
add comment

3 Answers

up vote 1 down vote accepted

It works fine for me. To reverse order you'd negate the original compareTo, or swap the variables, but not both.

We'll probably need to see the rest of the code to understand why you're seeing what you're seeing; I cut and pasted your code verbatim, so odds are good the issue lies elsewhere.


dump(theArray);
Arrays.sort(theArray, new Comparator<Integer[]>() {
    public int compare(Integer[] int1, Integer[] int2) {
        Integer numOfKeys1 = int1[1];
        Integer numOfKeys2 = int2[1];
        return numOfKeys1.compareTo(numOfKeys2);
    }
});
System.out.println("================");
dump(theArray);


0 10 
0 10 
1 9 
2 9 
3 9 
4 15 
5 10 
6 4 
7 8 
8 11 
9 12 
================
6 4 
7 8 
1 9 
2 9 
3 9 
0 10 
0 10 
5 10 
8 11 
9 12 
4 15 
share|improve this answer
    
Thanks for testing my sort() function. I'll post the rest of my code now. –  Drake Oct 29 '11 at 6:08
add comment

Code works for me too. Sorry for the messy code, i had to do a quick test. Regards!

import java.util.*;

class arraysort {

    public static Integer[][] mysort(Integer[][] ar) {
        Arrays.sort(ar, new Comparator<Integer[]>() {
            @Override
            public int compare(Integer[] int1, Integer[] int2) {
                Integer numOfKeys1 = int1[1];
                Integer numOfKeys2 = int2[1];
                return numOfKeys1.compareTo(numOfKeys2);
            }
        });
        return ar;
    }

    public static void main(String[] s) {
        Integer[][] myarr = {{0, 10}, {1, 9}, {2, 9}, {3, 9}, {4, 15}, {5, 10}, {6, 4}};

        for (Integer[] i : myarr) {
            System.out.println(i[0] + "," + i[1]);
        }

        myarr = mysort(myarr);

        for (Integer[] i : myarr) {
            System.out.println(i[0] + "," + i[1]);
        }
    }
}
share|improve this answer
    
Thank you, program is working now. –  Drake Oct 29 '11 at 7:56
add comment

I also ran your code entirely and it worked... one thing though I had to replace the

System.out.println(array[0][p] + " " + array[1][p]);

with

System.out.println(array[p][0] + " " + array[p][1]);

to run the code because otherwise it gives an array index out of bounds exception.

Negating the compareTo return is easier than swapping the values and allows easy change.

share|improve this answer
    
Thank you, my indices were mixed up. I've got my program working now. –  Drake Oct 29 '11 at 7:55
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.