0

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.

3
  • 2
    Store it by row. Change your display method to iterate by columns. Commented Mar 19, 2018 at 16:01
  • Then you can use docs.oracle.com/javase/7/docs/api/java/util/… Commented Mar 19, 2018 at 16:08
  • Alternatively, you can store your 2D array in a 1D array, by columns and use Arrays.sort(array, begin, end) Commented Mar 19, 2018 at 16:11

3 Answers 3

1

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]]

Sign up to request clarification or add additional context in comments.

Comments

0

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');
        }
    }

}

Comments

0

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];
    }
}

1 Comment

Doesn't work even if I fix the "j" and the int[] temp = int[numberofrows]

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.