using System;
class MatrixColumnSorter
{
static void SortColumns(string[,] matrix) {
int rows = matrix.GetLength(0);
int cols = matrix.GetLength(1);
for (int col = 0; col < cols; col++) {
// Extract column into an array
string[] column = new string[rows];
for (int row = 0; row < rows; row++) {
column[row] = matrix[row, col];
}
// Sort the column
Array.Sort(column);
// Place sorted values back into the matrix
for (int row = 0; row < rows; row++) {
matrix[row, col] = column[row];
}
}
}
static void PrintMatrix(string[,] matrix) {
int rows = matrix.GetLength(0);
int cols = matrix.GetLength(1);
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
Console.Write(matrix[row, col] + " ");
}
Console.WriteLine();
}
}
static void Main()
{
string[,] matrix = {
{"ccc", "zzzz", "x"},
{"eeee", "aaa", "ffff"},
{"uu", "hhh", "uuu"},
{"bbb", "gg", "yyyyyy"}
};
Console.WriteLine("Original Matrix:");
PrintMatrix(matrix);
SortColumns(matrix);
Console.WriteLine("\nSorted Matrix:");
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
*/