OrderBy
orders all elements in an enumerable by a certain key. In your example, the "elements" are arrays, and the "key" is the Nth element in the array (where N depends on
headerIndex
).
Here's an example:
object[][] jaggedArray = new object[][] {
new object[] { "Apples", 3.5, 200 },
new object[] { "Bananas", 4.8, 50 },
new object[] { "Ananas", 5.7, 75 }
};
var headerIndex = 0;
foreach (var row in jaggedArray.Skip(1).OrderBy(r => r[headerIndex]))
{
Console.WriteLine(string.Join(" | ", row));
}
This prints:
Ananas | 5.7 | 75
Bananas | 4.8 | 50
Because the first item, Apples, gets skipped, and the other items are sorted by
r[headerIndex]
(which is r[0] in the example, so the item name).