1

Need to do something like this in c # I do in 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());

I've been looking at the MSDN documentation, no method but to sort List, does not have anything to List<List<T>>.

thanks

Rawling
  • 49,248
  • 7
  • 89
  • 127
Jorge A C
  • 25
  • 3

3 Answers3

2

Jorge.

One posible solution:

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

Bye.

David
  • 61
  • 4
0

You can implement your own IComparer and sort or you can do ssomething with Linq which essentially will go through the whole matrix and sort it, or you can turn it into another data structure alltogether, something like a List> and then you can sort that easily.

Or something like this:

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

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

Community
  • 1
  • 1
dutzu
  • 3,883
  • 13
  • 19
  • Dutzu, thanks for the reply. These examples and I was watching but not what I need. Only I have to sort the outermost. greetings – Jorge A C Feb 27 '13 at 12:24
0

You can sort by the first element of each array (if that's what you're asking):

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]));
Matthew Strawbridge
  • 19,940
  • 10
  • 72
  • 93