import java.util.Arrays;

public class SingleThreadSorter {
    public static void sortColumns(int[][] matrix) {
        int rows = matrix.length;
        int cols = matrix[0].length;
        Integer[] indices = new Integer[cols];
        for (int i = 0; i < cols; i++)
            indices[i] = i;

        Arrays.sort(indices, (a, b) -> Integer.compare(matrix[0][b], matrix[0][a]));
        int[][] sorted = new int[rows][cols];
        for (int i = 0; i < cols; i++) {
            for (int j = 0; j < rows; j++) {
                sorted[j][i] = matrix[j][indices[i]];
            }
        }

        for (int i = 0; i < rows; i++)
            System.arraycopy(sorted[i], 0, matrix[i], 0, cols);
    }
}