Sorting a list based on another list or a different size
.net, c#, linq, sorting
Solution
Ordering by the index should work:
var result = sequence1.OrderBy(ordered.IndexOf);
However, if there might be items that are not in `ordered`, you'll need to do some extra processing:
var result = from n in sequence1
let i = ordered.IndexOf(n)
orderby i == -1 ? ordered.Count : i
select n
Problem
Consider the following lists: ``` List<string> ordered = new List<string> (new string [] { "one", "two", "three", ... }); List<string> sequence1 = new List<string> (new string [] { "two", "three", "ten", ... }); ``` The list `[ordered]` is a superset with all possible values. The list `[sequence1]` is a randomly-ordered subset of the `[ordered]` list. It may contain some or all of the super set's elements. I currently have a long-winded function that sorts `[sequence1]` based on `[ordered]` and was wondering if a simpler Linq way to do it. The current method iterates the super-set using a for loop and tries to find the value in the subset and moves it to its relevant position. The number of items in the list will never be more than a few dozen and the sorting operation will sparingly be called so efficiency is not key here.