Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simplest way to sort coordinates by y-value in Java?

Suppose that I have an (unsorted in any way) array:

[ 12 64 35 ]
[ 95 89 95 ]
[ 32 54 09 ]
[ 87 56 12 ]

I want to sort it so that the second column is in ascending order:

[ 32 54 09 ]
[ 87 56 12 ]
[ 12 64 35 ]
[ 95 89 95 ]

Here are the ways that I thought about dealing with this:

  1. Make each [ x y z ] value into a list, and correlate each xyz value with an identifier, whose property is the y-value of the xyz value. Then, sort the identifiers. (I'm not sure if sorting Java arrays keeps the corresponding values in that row)

  2. Use a hashmap to do the same thing as the previous

However, the above 2 methods are obviously somewhat wasteful and complicated since they rely on an external identifier value which is unneeded, so is there a simpler, faster, and more elegant way to do this?

Sorry if this is a dumb question, I'm not familiar at all with the way that Java sorts arrays.

like image 513
Yunyu Lin Avatar asked Jan 05 '13 19:01

Yunyu Lin


2 Answers

Easiest and most cleanest way would be to write a small comparator class. That will give you more flexibility in controlling the behavior of sorting also; for example you can sort 1st element or any element of arrays.

Comparator will be something like:

new Comparator(){

            public int compare ( Integer[] obj1, Integer[] obj2)
            {
                return obj1[1].compareTo(obj2[1]); 
            }
like image 131
Deepak Singhal Avatar answered Nov 15 '22 02:11

Deepak Singhal


This is a one-liner (if you count an anonymous class as one line), making use of Arrays.sort() and a suitably typed and coded Comparator:

Arrays.sort(grid, new Comparator<int[]>() {
    public int compare(int[] o1, int[] o2) {
        return o1[1] - o2[1];
    }
});

Note the simple comparison expression o1[1] - o2[1] - no need to unbox to Integer and use Integer.compareTo().

Here's a test with your data:

public static void main(String[] args) {
    int[][] grid = new int[][] { 
        { 12, 64, 35 },
        { 95, 89, 95 },
        { 32, 54,  9 },
        { 87, 56, 12 }};
    Arrays.sort(grid, new Comparator<int[]>() {
        public int compare(int[] o1, int[] o2) {
            return o1[1] - o2[1];
        }
    });
    System.out.println(Arrays.deepToString(grid).replace("],", "],\n"));
}

Output:

[[32, 54, 9],
 [87, 56, 12],
 [12, 64, 35],
 [95, 89, 95]]



Just for fun, here it is literally as one line:

Arrays.sort(grid, new Comparator<int[]>() {public int compare(int[] o1, int[] o2) {return o1[1] - o2[1];}});
like image 37
Bohemian Avatar answered Nov 15 '22 02:11

Bohemian