Sort one list by another

前端 未结 7 1408
借酒劲吻你
借酒劲吻你 2020-12-02 05:59

I have 2 list objects, one is just a list of ints, the other is a list of objects but the objects has an ID property.

What i want to do is sort the list of objects b

7条回答
  •  悲哀的现实
    2020-12-02 06:18

    Join is the best candidate if you want to match on the exact integer (if no match is found you get an empty sequence). If you want to merely get the sort order of the other list (and provided the number of elements in both lists are equal), you can use Zip.

    var result = objects.Zip(ints, (o, i) => new { o, i})
                        .OrderBy(x => x.i)
                        .Select(x => x.o);
    

    Pretty readable.

提交回复
热议问题