farzin parsa
farzin parsa

Reputation: 547

Sort a list of double[,] based on first element of array

I have a list of 2d arrays. Now I want to sort my list *based on* only 1st element of my array - not the second element. I wrote the code as following:

List<double[,]> lsResultsOfEq = new List<double[,]>();
double[,] resultOfEqConv;

for (int i = 0; i < n; i++)
{
    resultOfEq = a*b*c;

    //add value and index
    resultOfEqConv = new double[1, 2];
    resultOfEqConv[0, 0] = (double)resultOfEq[0, 0];
    resultOfEqConv[0, 1] = i;
    lsResultsOfEq.Add(resultOfEqConv);
}  

Now when I use the sort function I get the error "Failed to compare two elements in the array" how should I set my sortlist that only do the sorting based on first elemen of array.

Upvotes: 3

Views: 2334

Answers (2)

Felipe Oriani
Felipe Oriani

Reputation: 38598

Try using OrderBy method from , for sample:

include the namespace:

using System.Linq;

and try this:

var orderResult = lsResultsOfEq.OrderBy(x => x[0, 0]).ToList();

Upvotes: 4

fox
fox

Reputation: 31

The straight forward way would be using as pointed out by Felipe Oriani.

Just in case you are looking at List.Sort() for in-place sorting. You may try:

lsResultsOfEq.Sort((x, y) => x[0, 0].CompareTo(y[0, 0]));

Upvotes: 3

Related Questions