Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class DynamicSorter
- {
- public string Name, Rank;
- public int Year;
- public DynamicSorter(string name, string rank, int year = 1988)
- {
- Name = name;
- Rank = rank;
- Year = year;
- }
- public override string ToString()
- {
- return $"{Name}:{Rank}:{Year}";
- }
- /// <summary>
- /// This function satisfies: Make the sort dynamic per https://social.msdn.microsoft.com/Forums/vstudio/en-US/f66b4d1d-231a-4ed9-b009-bb89db450d4f/linq-with-dynamic-order-by-clause-c?forum=csharpgeneral and https://docs.microsoft.com/en-us/dotnet/api/system.linq.igrouping-2?view=netframework-4.8 and https://stackoverflow.com/questions/2728340/how-can-i-do-an-orderby-with-a-dynamic-string-parameter
- /// </summary>
- /// <param name="input"></param>
- /// <param name="firstSort"></param>
- /// <returns></returns>
- public static List<DynamicSorter> SorterFunction(List<DynamicSorter> input, string firstSort)
- {
- //TODO: Implement "thenBy"
- //TODO: Implement: var sortedItems = reverse ? items.OrderByDescending(orderByFunc) : items.OrderBy(orderByFunc)
- Func<DynamicSorter, Object> orderByFunc_First = null;
- switch (firstSort)
- {
- case "Rank":
- orderByFunc_First = item => item.Rank;
- break;
- case "Name":
- orderByFunc_First = item => item.Name;
- break;
- case "Year":
- orderByFunc_First = item => item.Year;
- break;
- default:
- orderByFunc_First = item => null;
- return input;
- }
- return input.OrderBy(orderByFunc_First).ToList();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement