Reputation: 157
A list should be ordered by a property which is sub property of a object in a list.
pList = pList
.OrderBy(x => x.GetType()
.GetProperty(sortBasedValue)
.GetValue(x, null))
.ToList();
Will sort all Elements in the list, but some subelements have multiple properties.
Already tried subitem.value
as sortBasedValue
but will not work.
Upvotes: 1
Views: 117
Reputation: 14477
GetProperty takes the name of the property as parameter, but you can't use it to fletch its property's property directly. You would need to chain it.
Enumerable.Empty<object>()
// looks from the naming its a private variable,
//so you might want to call it via , GetProperty("subitem", BindingFlags.NonPublic)
.OrderBy(x =>
{
var subitem = x.GetType().GetProperty("subitem").GetValue(x);
return subitem.GetType().GetProperty("value").GetValue(subitem);
})
.ToList();
If you have the type of the pList
, I would suggest you to use a property selector aka Func<TObject, TPropertyToOrderBy>
and give it to the OrderBy
.
Upvotes: 1
Reputation: 76
You need to implement a custom IComparer and use the sort method.
The solution is here C# Sort and OrderBy comparison
Upvotes: 0