I have a 2D gear array. And I want to sort it by line.
I searched and found code to sort 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 to sort by any line?
Any help is appreciated.
Sample of my gear 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 and @Martin I want my cog array to sort capm [2] [].
stereo
source share