2013-02-27 227 views
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<T>>

感谢

+0

什么是排序后'matrix'您所需的布局,为什么? – Rawling 2013-02-27 12:29:11

回答

2

豪尔赫。

一个更多钞票溶液:

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

再见。

+0

太棒了,这是我所需要的。 – 2013-02-27 12:36:15

0

您可以实现自己的IComparer和排序,或者你还可以使用LINQ ssomething基本上会经过整个矩阵和排序,或者你可以把它变成另一种数据结构产品总数,有点像列表>,然后你可以轻松地排序。

或者是这样的:

How do I sort a two-dimensional array in C#?

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

+0

Dutzu,谢谢你的回复。这些例子和我正在观看,但不是我需要的。只有我必须排序最外层。 问候 – 2013-02-27 12:24:14

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