Sort a list by a custom order

后端 未结 3 2033
忘了有多久
忘了有多久 2020-12-12 02:35

I have an array of some types

private string[] linkTypes = {
    \"dog\",
    \"cat\",
    // and so on ..
};

Yes, I could u

3条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-12 03:05

    Assuming linkTypes (the private string array) is in the same class as links (the list of LinkElement), you can use LINQ's OrderBy with a simple lambda expression:

    var sortedLinks = links.OrderBy(le => Array.IndexOf(linkTypes, le.linkType)).ToList()
    

提交回复
热议问题