If you want to compare by their index in your SortedIds
list, you can do:
public class IdComparer : IComparer<Item> {
private readonly Dictionary<int, int> idIndexes;
public IdComparer(IEnumerable<int> sortedIds)
{
idIndexes = sortedIds
.Select((id, idx) => new { Id = id, Index = idx })
.ToDictionary(p => p.Id, p.Index);
}
public int Compare(Item x, Item y) {
xIndex = idIndexes[x.Id];
yIndex = idIndexes[y.Id]
return xIndex.CompareTo(yIndex);
}
}
However you could use linq:
IEnumerable<Item> sorted = SortedIds
.Select((id, idx) => new { Id = id, Index = idx })
.Join(items, p => i.Id, item => item.Id, (p, item) => new { Item = item, Index = idx })
.OrderBy(p => p.Index)
.Select(p => p.Item);