排序二维数组排在他们的第一要素升序排列在c# [英] Sort 2d array rows in ascending order of their first elements in c#

查看:289
本文介绍了排序二维数组排在他们的第一要素升序排列在c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要在他们的第一要素,依次递增二维数组行像例如排序

I need to sort 2d array rows in ascending order of their first elements, like in example

{{5,7,6},{2,9,6},{4,8,1}} - > {{2,9,6},{4,8,1},{5 ,7,6}}

{{5,7,6},{2,9,6},{4,8,1}} --> {{2,9,6},{4,8,1},{5,7,6}}

我可以找到行的最大因素,但我现在不如何对行进行排序。

I can find max element in row, but i don't now how to sort the rows.

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; 
}

您能不能指点什么?

在此先感谢!

推荐答案

不幸的是与sintax你缺少 LINQ 至极的力量是最好的组件之一 Net框架你可以试试这个代替

Sadly with that sintax you are missing the power of linq wich is one of the best components of .Net framework you can try this instead

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]);

这篇关于排序二维数组排在他们的第一要素升序排列在c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆