score:13

Accepted answer

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

score:3

Use the index as sort criteria, this is without LINQ:

sequence1.Sort
(
    (x, y) =>
    {
        return ordered.IndexOf(x).CompareTo(ordered.IndexOf(y));
    }
);

You could take from ordered only those that exists in sequence1:

from x in ordered where sequence1.Contains(x) select x

Though, this approach is less efficient.


These approaches has ben tested on LINQPad.


Related Articles