I have a list of complex objects and would like to order it with linq (to objects) according to an "order-by-keyword":
protected override sealed List<IndexViewModel> Order(List<IndexViewModel> listToOrder, string orderBy)
{
List<IndexViewModel> indexViewModels;
switch (orderBy.ToLower())
{
case "creator":
indexViewModels = listToOrder.OrderBy(s => s.Creator).ToList();
break;
case "startdate":
indexViewModels = listToOrder.OrderBy(s => s.StartDate).ToList();
break;
...
default:
indexViewModels = listToOrder.OrderBy(s => s.CreationDate).ToList();
break;
}
return indexViewModels;
}
Is there any way to improve my code? Probably by assigning my keyword to linq and let it decide how to sort it?