List sort based on another list

List.FindIndex() is your friend when listA is small and already sorted:

var orderedB = listB.OrderBy(b => listA.FindIndex(a => a.id == b.id));

Working example: https://dotnetfiddle.net/CpLeFU

As @goodeye pointed out in the comments, performance will be a nightmare on larger lists. Use the accepted answer in that case.


You should be able to use a join to produce your desired output. Example using query syntax.

var orderedOptions = from option in options_list
                     join type in types_list
                     on option.Type_ID equals type.ID
                     orderby type.Ordering
                     select option;

Tags:

C#

Linq

Sorting