How can I sort this list of objects based on the property I choose?
empList: TList<TEmployee>;
empList.Add(TEmployee.Create('Bob', 1000);
empList.Add(TEmployee.Create('Alice', 2000);
empList.Add(TEmployee.Create('Mary', 40000);
empList.Add(TEmployee.Create('Jack', 500);
//Sorting code
CustomSort(empList, name); //Sort by name
CustomSort(empList, salary); //Sort by salary
- Alice ($ 2000)
- Bob ($ 1000)
- Jack ($ 500)
- Mary ($ 40000)
Sorted by salary:
- Jack ($ 500)
- Bob ($ 1000)
- Alice ($ 2000)
- Mary ($ 40000)
I know there is a way to make it work, but it is slow and manual (comparing each object and sorting).
There is no need to pass a "decrescent" parameter (I can just list.Reverse
if i want to).