Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to simultaneously sort 2 lists using LINQ?

Tags:

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?

like image 691
Simon Avatar asked May 18 '12 15:05

Simon


1 Answers

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(); 
like image 134
Jon Skeet Avatar answered Nov 07 '22 17:11

Jon Skeet