#include <iostream>
#include <algorithm>
#define COLS 4
int row_to_sort = 0;
int matrix[COLS][COLS] = { {2, 8, 1, 4},
{7, 0, 9, 8},
{2, 1, 6, 3},
{3, 8, 5, 6} };
void print_matrix(int matrix[][COLS], int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
std::cout << matrix[i][j] << " ";
}
std::cout << "\n";
}
}
int main() {
int col_indexes[COLS] = { 0 };
for (int i = 0; i < COLS; i++) {
col_indexes[i] = i;
}
std::sort(col_indexes, col_indexes + COLS, [] (const int &a, const int &b) {
return matrix[row_to_sort][a] < matrix[row_to_sort][b];
});
int result[COLS][COLS] = {{ 0 }};
for (int i = 0; i < COLS; i++) {
for (int j = 0; j < COLS; j++) {
result[i][j] = matrix[i][col_indexes[j]];
}
}
print_matrix(result, COLS, COLS);
}
/*
run:
1 2 4 8
9 7 8 0
6 2 3 1
5 3 6 8
*/