2

I have a List products; with 100+ items.

For each item from the list I have product id.

And I have an array int[] sortedArray = {3,6,22,98,6,45,87};

I have to sort my product list and make order by productid, how it is in sortedArray. How can I implement that?

Thanks in Advance!

test_
  • 406
  • 3
  • 11
Nidhi Tank
  • 35
  • 8

1 Answers1

2

One possible approach could be using OrderBy method by passing a Func which uses IndexOf method in order to sort the list based on the sortedArray.

list.OrderBy(s => sortedArray.IndexOf(s.ProductId)).ToList();
Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128