#include <iostream>
#include <vector>
#include <algorithm>
void sortColumns(std::vector<std::vector<std::string>>& matrix) {
int rows = matrix.size();
int cols = matrix[0].size();
for (int col = 0; col < cols; col++) {
// Extract column into a vector
std::vector<std::string> column;
for (int row = 0; row < rows; row++) {
column.push_back(matrix[row][col]);
}
// Sort the column
std::sort(column.begin(), column.end());
// Place sorted values back into the matrix
for (int row = 0; row < rows; row++) {
matrix[row][col] = column[row];
}
}
}
void printMatrix(const std::vector<std::vector<std::string>>& matrix) {
for (const auto& row : matrix) {
for (const auto& elem : row) {
std::cout << elem << " ";
}
std::cout << "\n";
}
}
int main() {
std::vector<std::vector<std::string>> matrix = {
{"ccc", "zzzz", "x"},
{"eeee", "aaa", "ffff"},
{"uu", "hhh", "uuu"},
{"bbb", "gg", "yyyyyy"}
};
std::cout << "Original Matrix:\n";
printMatrix(matrix);
sortColumns(matrix);
std::cout << "\nSorted Matrix:\n";
printMatrix(matrix);
}
/*
run:
Original Matrix:
ccc zzzz x
eeee aaa ffff
uu hhh uuu
bbb gg yyyyyy
Sorted Matrix:
bbb aaa ffff
ccc gg uuu
eeee hhh x
uu zzzz yyyyyy
*/