So I know you can use OrderBy like so with LINQ:
MyList.OrderBy(x => x.foo[0].Value).ThenByDescending(x => x.foo[1].Value);
However I am in a situation where I do not know how many properties x.foo will have and I would like to order by all of them. Is this possible?
EDIT: In this instance x.foo is a list of KeyValuePair's where I am sorting by the Value in order of the Keys.
Provided there's at least one item and all the items have the same number of 'foos'
int foosPerItem = MyList.First().Foo.Count;
IOrderedEnumerable<T> ordered = MyList.OrderBy(x => x.foo[0].Value);
for (int i = 1; i < foosPerItem; i++)
{
ordered = ordered.ThenByDescending(x => x.foo[i].Value);
}
// added after your comment:
// ordered = ordered.ThenByDescending(x => x.bar);
Following your other comment about ordering by bar first:
IOrderedEnumerable<T> ordered = MyList.OrderBy(x => x.bar);
for (int i = 0; i < foosPerItem; i++)
{
ordered = ordered.ThenByDescending(x => x.foo[i].Value);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With