One thing you should be aware of: sorting a jagged array, an array of the form SomeArray[][] by columns is very easy to do with Linq; sorting a two-dimensional Array, an array of the form SomeArray[,] by columns requires nested iteration. See Marc Gravell's comments on this on StackOverFlow: [
^], and, also, see: [
^].
Technically, the problem with achieving column sort in a multi-dimensional array using Linq is related to the fact that it does not implement/support IEnumerable; a jagged array does.
I would approach this problem by making use of generic collections, and Linq. Here's a quick sketch:
using System.Collections.Generic;
using System.Linq;
private void TestSort()
{
int[,] ints = new int[3,3]
{
{2, 2, 1}, {3, 53, 4}, {32, 5, 3}
};
List<List<int>> columns = new List<List<int>>();
for (int i = 0; i < 3; i++)
{
List<int> column = new List<int>();
for (int j = 0; j < 3; j++) column.Add(ints[j,i]);
column.Sort();
columns.Add(column);
}
List<List<int>> rows = new List<List<int>>();
for (int i = 0; i < 3; i++)
{
List<int> row = new List<int>();
for (int j = 0; j < 3; j++) row.Add(columns[j][i]);
row.Sort();
rows.Add(row);
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++) ints[i, j] = rows[i][j];
}
}
I suggest you set break-points, and execute this code and examine the contents of the different Lists as they are defined, and sorted.
Yes, this is a "brute force" solution, and, with effort, I am sure one could make it "leaner and meaner."