Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Sort List<> with associated array

I'd like to sort a List<string>, but at the same time sort a float[] array: which are the associated values. (I know this is bad design, it is so historically). So whenever two items are swapped in the List I would like the values at the indexes in the array also swapped. There's a method for doing this with two arrays: http://msdn.microsoft.com/en-us/library/aa311223%28v=vs.71%29.aspx However I can't find anything for a List and an array: is there such a method somewhere?

like image 950
user968698 Avatar asked Jun 28 '26 02:06

user968698


1 Answers

You can use Zip (.NET 4.0 and above) to merge the list and array and perform the sort on the result.


Update:

Since you are using .NET 3.5, you can use Array.Sort, as you have linked to, converting the list to an array first:

var stringArray = myStringList.ToArray();
Array.Sort(stringArray, myFloatArray);

The way I would normally do this, however, would be with creating a type holding a string and float, and use that in a generic collection to sort.

like image 98
Oded Avatar answered Jun 30 '26 16:06

Oded



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!