Unfortunately a multi-dimensional array is a quite static beast. You can easily access single elements within it by providing the n-dimensional coordinates (eg. array[2,8,4] in a 3d array), but you can't access any whole areas (like rows, columns, rectangles, etc.).
If you really have the need to do such a kind of re-sorting you shouldn't save your data within a multi-dimensional array. Instead put it into a jagged array or a list of lists or even a IReadOnlyDictionary<int, IReadOnlyDictionary<int, IReadOnlyDictionary<int, int>>> for a 4 dimensions sparse matrix. In that case you could quite easily write a comparer for each dimension as you like.
In your case you just need a something like List<IReadOnlyList<int>> and a IComparer<IReadOnlyList<T>> which could possibly be implemented like this:
public class ListComparer<T> : IComparer<IReadOnlyList<T>>
{
public static readonly IComparer<IReadOnlyList<T>> Default = new ListComparer<T>();
private readonly bool _checkCount;
private readonly int _numberOfElementsToCompare;
private readonly IComparer<T> _elementComparer;
public ListComparer()
: this(true, 1, Comparer<T>.Default)
{
}
public ListComparer(
bool checkCount,
int numberOfElementsToCompare,
IComparer<T> elementComparer)
{
_checkCount = checkCount;
_numberOfElementsToCompare = numberOfElementsToCompare;
_elementComparer = elementComparer
?? throw new ArgumentNullException(nameof(elementComparer));
}
public int Compare(IReadOnlyList<T> x, IReadOnlyList<T> y)
{
if (ReferenceEquals(x, y))
return 0;
if (ReferenceEquals(x, null))
return -1;
if (ReferenceEquals(y, null))
return 1;
if (_checkCount)
{
var diff = x.Count.CompareTo(y.Count);
if (diff != 0)
return diff;
}
return x.Take(_numberOfElementsToCompare)
.Zip(y, (i, j) => _elementComparer.Compare(i, j))
.SkipWhile(value => value == 0)
.FirstOrDefault();
}
}
And would be used:
var matrix = new List<IReadOnlyList<int>>
{
{ new List<int> { 1, 2, 3, 4 } },
{ new List<int> { 5, 6, 7, 8 } },
{ new List<int> { 2, 3, 4, 5 } }
};
matrix.Sort(ListComparer<int>.Default);
foreach (var item in matrix)
{
Console.WriteLine(String.Join(", ", item));
}
{1,2,1}and{1,1,2}which one is smaller?Array.Sortsupports only one dimentional arrays, so that will not work anyway.int[3][4]but your code is about multi-dimensional arraysint[3,4]. I suppose your code is your truth and the text is just misspelled.