RageMasterGaming
RageMasterGaming

Reputation: 77

How to sort a 2D integer array by columns

If we have a 2D int array that looks like this:

6 | 8 | 9 | 16 
0 | 6 |-3 | 4
18| 2 | 1 | 11

Than the expected output would be:

 0 | 2 |-3 | 4 
 6 | 6 | 1 | 11
 18| 8 | 9 | 16

I block when when i think of it how to sort vertically.

int[][] array = new int[10][10]; 
for(int i = 0; i < array.length; i++){
    for(int j = 0; j < array[0]; j++){
       //here i block because i don't know how i would vertically sort them
    }
}

I know there are a lot of topics about this and in all of them not one of them worked for me. Therefore I apologize for this post but I am stuck.

Upvotes: 0

Views: 141

Answers (3)

OldCurmudgeon
OldCurmudgeon

Reputation: 65793

You can make your own class that makes a List<T> from one column of the array and uses the array as the backing data (i.e. implement set). You can then use Collections.sort to sort it in-place.

class ColumnList<T> extends AbstractList<T> implements List<T> {
    private final T[][] array;
    private final int column;

    public ColumnList(T[][] array, int column) {
        this.array = array;
        this.column = column;
    }

    @Override
    public T get(int index) {
        return array[index][column];
    }

    @Override
    public T set(int index, T element) {
        return array[index][column] = element;
    }

    @Override
    public int size() {
        return array.length;
    }
}

public void test(String[] args) {
    Integer[][] array = {
            {6, 8, 9, 16},
            {0, 6, -3, 4},
            {18, 2, 1, 11}
    };
    System.out.println("Before: " + Arrays.deepToString(array));
    // Sort each column separately.
    for (int i = 0; i < array[0].length; i++) {
        ColumnList<Integer> column = new ColumnList<>(array, i);
        Collections.sort(column);
    }
    System.out.println("After:  " + Arrays.deepToString(array));
}

prints

Before: [[6, 8, 9, 16], [0, 6, -3, 4], [18, 2, 1, 11]]

After: [[0, 2, -3, 4], [6, 6, 1, 11], [18, 8, 9, 16]]

Upvotes: 1

vincrichaud
vincrichaud

Reputation: 2208

You can use a temp array that will contain a column and use the method sort on it.

int[][] input = new int[numberOfRow][numberOfColumn]; 
int[][] result = new int[numberOfRow][numberOfColumn];
for(int col = 0; col < numberOfColumn; col++){
    for(int row = 0; row < numberOfRow; row++){
       int[] temp = int[numberOfRow];
       temp[row] = input[row][col];
    }
    Arrays.sort(temp);
    for(int i=0; i<numberOfColumn; i++){
        result[i][col] = temp[i];
    }
}

Upvotes: 0

Victor Gubin
Victor Gubin

Reputation: 2937

Learn Linear algebra please:

An example with the matrix transpose

public class MatrixSort {

    private static final int MATRIX[][] = { 
              { 6, 8, 9, 16 }, 
              { 0, 6, -3, 4 }, 
              { 18, 2, 1, 11 } 
     };

    private static int[][] transpose(int[][] m) {
        int[][] ret = new int[m[0].length][m.length];
        for (int i = 0; i < m.length; i++)
            for (int j = 0; j < m[0].length; j++)
                ret[j][i] = m[i][j];
        return ret;
    }

    public static void main(String[] args) {
        int ret[][] = transpose(MATRIX);
        for(int i=0; i < ret.length; i++) {
            Arrays.sort(ret[i]);
        }
        ret = transpose(ret);
        for(int i=0; i < ret.length; i++) {
            for(int j=0; j < ret[i].length; j++) {
                System.out.print(ret[i][j]);
                System.out.print(" | ");
            }
            System.out.print('\n');
        }
    }

}

Upvotes: 0

Related Questions