c# - Sorting a dynamic object list based on another list -
i have 2 lists. 1 dynamic list of objects. list of strings. want sort object list based on other list.
list<dynamic> list1; // object1,object2,object3,object4 list <string> list2; //"abc","bcd","da"
these objects has 1 of attributes "alphabets" on basis has sorted.
the objects may not equal number of elements in second list.
something might work, if indexes of 2 lists align how want them to. you'd have ensure lists have same length work correctly though.
var result = list1 .select((item, index) => new { item = item, order = list2[index] }) .orderby(x => x.order) .select(x => x.item);
if aren't same length, criteria order? undefined problem. 1 approach put them @ end of list.
var result = list1.take(list2.length) .select((item, index) => new { item = item, order = list2[index] }) .orderby(x => x.order) .select(x => x.item); var concatted = result.concat(list1.skip(list2.length));
Comments
Post a Comment