Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort list of double array

I have this dictionary

Dictionary<string, List<double[]>> dictionary = new Dictionary<string, List<double[]>>();

I filled it with a lot of data.

each array in the list has two values like this:

[xvalue, yvalue]

Is there anyway so I can sort each list in the dictionary depending on the xvalue of the arrays.

For example

This is the list

[[1,100], [10, 100], ....[3, 100]]

After sorting should be this

[[1,100], [3,100], [10,100]]

To be honest, All I did is google searching for such a feature but I couldn't do more than that because I am not that good in c# and I thought asking you would save a lot of time.

like image 555
Marco Dinatsoli Avatar asked Nov 24 '25 15:11

Marco Dinatsoli


2 Answers

Iterate over all element of the dictionary and sort each of the lists. You can sort a list by calling List.Sort with a proper comparison function.

foreach(var entry in dictionary) {
    entry.Value.Sort((a, b) => a[0].CompareTo(b[0]));
}
like image 86
Danvil Avatar answered Nov 27 '25 06:11

Danvil


Try this code:

foreach (var pair in dictionary) 
{
    pair.Value.Sort((x, y) => x[0].CompareTo(y[0]));
}
like image 35
tukaef Avatar answered Nov 27 '25 05:11

tukaef



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!