I have 2 lists, one is the original list and the other is a sorted list. I want to sort the first list by the second list, but my method will not return anything, so it must sort the list in place.
I did this method:
public void SortByList(IEnumerable<JObject> source, IEnumerable<JObject> products)
{
var ids = source.Select(m => m["_id"].ToString()).ToList();
products.OrderBy(m => ids.IndexOf(m["_id"].ToString()));
}
But as you know, OrderBy creates a new list. How can I Sort using the ids?
Comparison<T>delegate: stackoverflow.com/a/3309292/860585IComparer<T>. But that's not going to work while your parameter isIEnumerable<JObject>- can you change it to aList<JObject>?public IEnumerable<JObject> ...and inside of the method, you write actual code. But instead of returning a list, you useyield returnto return a single entry. C# will do all the magic required to make it anIEnumerable<>to further enhance, you can then make the method a static extension by making the method static and adding athisin front of sourceIEnumerable. Otherwise in order to sort it you would have to convert it to a concrete type, e.g.List<JObject>.