I have 2D jagged array. And I want to sort it by any rows.
I've searched and found code for sorting by columns
private static void Sort<T>(T[][] data, int col)
{
Comparer<T> comparer = Comparer<T>.Default;
Array.Sort<T[]>(data, (x,y) => comparer.Compare(x[col],y[col]));
}
Can I adapt it for sort by any rows ?
Any help is appreciated.
Sample of my jagged array (Added)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
int n = 10;
int[][] capm = new int[3][];
for (int i = 0; i <= 2; i++)
{
capm[i] = new int[n + 1];
}
Random rand = new Random();
for (int i = 1; i <= n; i++)
{
capm[1][i] = i;
}
for (int i = 1; i <= n; i++)
{
capm[2][i] = rand.Next(1, 6);
}
Sort(capm, 2);
Console.ReadLine();
}
private static void Sort<T>(T[][] data, int col)
{
data = data.OrderBy(i => i[col]).ToArray();
}
}
}
@Dani & @Martin I want my jagged array to sort by capm[2][].