I have two lists { 7 3 5 } and {9 8 1}.
I want to sort my first list and I want the second list to have the same index permutation as given by the first list.
{3 5 7} => {8 1 9}
Is it possible to do this in a single LINQ statement?
I have two lists { 7 3 5 } and {9 8 1}.
I want to sort my first list and I want the second list to have the same index permutation as given by the first list.
{3 5 7} => {8 1 9}
Is it possible to do this in a single LINQ statement?
Sounds like you might want:
var list1 = new List<int> { 7, 3, 5 };
var list2 = new List<int> { 9, 8, 1 };
var orderedZip = list1.Zip(list2, (x, y) => new { x, y } )
.OrderBy(pair => pair.x)
.ToList();
list1 = orderedZip.Select(pair => pair.x).ToList();
list2 = orderedZip.Select(pair => pair.y).ToList();
You could try using the Zip method:
var sortedPairs = list1
.Zip(list2, (item1, item2) => new KeyValuePair<int, int>(item1, item2))
.OrderBy(pair => pair.Key);
Then you could get the first sorted list by:
var sortedList1 = sortedPairs.Select(pair => pair.Key);
And the second list by:
var sortedList2 = sortedPairs.Select(pair => pair.Value);