1

需要在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 没有任何方法,没有任何List<List<T>>.

谢谢

4

3 回答 3

2

乔治。

一种可能的解决方案:

matrix.Sort(delegate(List<double> l1, List<double> l2)
            {
               return l1[0].CompareTo(l2[0]);
            });

再见。

于 2013-02-27T12:34:40.957 回答
0

您可以实现自己的 IComparer 并进行排序,或者您可以使用 Linq 做一些事情,这基本上会遍历整个矩阵并对其进行排序,或者您可以将其全部转换为另一个数据结构,例如 List>,然后您可以轻松地进行排序.

或者是这样的:

如何在 C# 中对二维数组进行排序?

http://www.codeproject.com/Tips/166236/Sorting-a-two-dimensional-array-in-C

于 2013-02-27T12:08:34.813 回答
0

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

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]));
于 2013-02-27T12:36:04.267 回答