There are a few tings wrong with your code as it currently stands. If you look at the docs for IComparer<T>
you'll see that the T
is what you're saying you are going to compare. In your code this is Test
but you go on to code for comparisons of What
- this means that your code will not compile. See here - error message is:
'Rextester.What' does not implement interface member 'System.Collections.Generic.IComparer.Compare(Rextester.Test, Rextester.Test)'
(Ignore the "Rextester" bit there!).
With all that said and done, you should implement a WhatComparer
:
public class WhatComparer : IComparer<What>
{
private List<int> orderBy;
public WhatComparer(List<int> orderBy)
{
this.orderBy = orderBy;
}
public int Compare(What x, What y)
{
return orderBy.IndexOf(x.ID).CompareTo(orderBy.IndexOf(y.ID));
}
}
And use that for ordering:
WhatList.Sort(new WhatComparer(OrderByList));
Live example: http://rextester.com/BZKO33641