Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用比较列表对列表进行排序_C#_List_Sorting - Fatal编程技术网

C# 使用比较列表对列表进行排序

C# 使用比较列表对列表进行排序,c#,list,sorting,C#,List,Sorting,需要在c中执行类似操作#我在java中执行: Double[][] matrix = new Double[3][4]; matrix[0][0] = 100.0; matrix[0][1] = -3.0; matrix[0][2] = 50.0; matrix[0][3] = 50.3; matrix[1][0] = 1.2; matrix[1][1] = 1.1; matrix

需要在c中执行类似操作#我在java中执行:

Double[][] matrix = new Double[3][4];

        matrix[0][0] = 100.0;
        matrix[0][1] = -3.0;
        matrix[0][2] = 50.0;
        matrix[0][3] = 50.3;

        matrix[1][0] = 1.2;
        matrix[1][1] = 1.1;
        matrix[1][2] = 0.9;
        matrix[1][3] = 10000.0;

        matrix[2][0] = 2.3;
        matrix[2][1] = 2.35;
        matrix[2][2] = 2.32;
        matrix[2][3] = 2.299;

        Arrays.sort(matrix, new Lexical());
我一直在看MSDN文档,除了对列表进行排序之外没有其他方法,没有任何东西可以
List


谢谢

你可以实现你自己的IComparer和sort,或者你可以用Linq做一些方法,它基本上会遍历整个矩阵并对它进行排序,或者你可以将它全部转换成另一个数据结构,比如列表>,然后你就可以轻松地排序了

或者像这样:

乔治

一个可行的解决方案:

matrix.Sort(delegate(List<double> l1, List<double> l2)
            {
               return l1[0].CompareTo(l2[0]);
            });
matrix.Sort(委托(列表l1、列表l2)
{
返回l1[0]。比较(l2[0]);
});

再见。

您可以按每个数组的第一个元素进行排序(如果这是您的要求):


Dutzu,谢谢你的回复。这些例子我都在看,但不是我需要的。只是我得把最外面的分类。问候排序后,您想要的矩阵布局是什么?为什么?太好了,这就是我需要的。
var matrix = new double[][] {
    new[] { 100.0, -3.0, 50.0, 50.3 },
    new[] { 1.2, 1.1, 0.9, 10000.0 },
    new[] { 2.3, 2.35, 2.32, 2.299}
};

Array.Sort(matrix, (a, b) => a[0].CompareTo(b[0]));