Jorge A C
Jorge A C

Reputation: 25

sort a list using a comparison list

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

Upvotes: 1

Views: 127

Answers (3)

Matthew Strawbridge
Matthew Strawbridge

Reputation: 20640

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

Upvotes: 0

David
David

Reputation: 61

Jorge.

One posible solution:

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

Bye.

Upvotes: 2

dutzu
dutzu

Reputation: 3910

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

Upvotes: 0

Related Questions