var data = read();
switch (selector)
{
case 1://accending
data = data.OrderBy(inner => inner[2]).ToArray();
drawChart();
for (int i = 0; i < data[0].Length; i++)
{
for (int j = 0; j < data.Length; j++)
{
if (j != 0)
{
Console.Write(" ");
}
Console.Write(data[j][i] + " ");
}
Console.WriteLine();
}
complete = true;
break;
case 2://decending
complete = true;
break;
default:
Console.WriteLine("Not an option please enter a number between 1 and 2");
break;
}
我有一个名为数据的交错数组,正在此处打印,我需要能够根据用户输入(案例 1 和 2)对数组进行排序
假设数据看起来像这样
jaggedarray[0] = new int[5] { 99, 999, 49, 79, 59 };
jaggedarray[1] = new int[3] { 199, 1999, 149 };
jaggedarray[2] = new int[2] { 999, 500 };
我如何对其进行排序,以便将第二列从最高到最低排序,然后打印出整个数组?
最佳答案
Hari Prasad 回答的另一种方法,非常好。
您可以使用 Array.Sort
进行排序,并使用针对第二列的 Comparison
。
像这样:
var jaggedarray = new int[3][];
jaggedarray[0] = new int[5] { 99, 999, 49, 79, 59 };
jaggedarray[1] = new int[3] { 199, 1999, 149 };
jaggedarray[2] = new int[2] { 999, 500 };
Array.Sort(jaggedarray, new Comparison<int[]>(
(x,y) => { return x[1] < y[1] ? -1 : (x[1] > y[1] ? 1 : 0); }
));
关于c# - 排序锯齿状数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36746218/