我需要按第一个元素的升序对二维数组行进行排序,如示例所示
{{5,7,6},{2,9,6},{4,8,1}} --> {{2,9,6},{4,8,1},{5,7, 6}}
我可以在行中找到最大元素,但我现在不知道如何对行进行排序。
public double[] maxInRow(double[,] n)
{
double[] result = new double[n.GetLength(0)];
for (int i = 0; i < n.GetLength(0); i++)
{
double max = 0;
for (int j = 0; j < n.GetLength(1); j++)
{
if (max < n[i,j])
{
max = n[i,j];
}
}
result[i] = max;
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
你能给点建议吗?
提前致谢!
可悲的是,有了这个语法,你就失去了它的力量linq,它是最好的组成部分之一,.Net framework你可以尝试这个
double[][] x = new double[2][];
x[0] = new double[] { 5, 2, 5 };
x[1] = new double[] { 6, 8, 3 };
x[2] = new double[] { 8, 3, 6 };
var sortedByFisrtVal = x.OrderBy(y => y[0]);
var sortedBySecondVal = x.OrderBy(y => y[1]);
//trying to guess maybe this is better
var sorted = x.OrderBy(y => y[0]).ThenBy(y => y[1]).ThenBy(y => y[2]);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9377 次 |
| 最近记录: |