I have a method where I pass in a List, which is then sorted in the method.
The type ChartItemData includes properties like Average, ProportionHighScore, and ProportionLowScore. Depending on method usage, I need to sort the List inside the method on different properties, and either Ascending or Descending.
How can I specify in the parameter list of the method which property to sort on, and which sortorder to use?
I imagine I could set up an Enum for SortDirection, but I still need to find out how to pass in the property to sort on. Here is some pseudocode to illustrate what I'm after, using List.OrderBy. I could also sort the List in place with the List.Sort method, if that makes more sense.
public enum SortDirection { Ascending, Descending }
public void myMethod(List<ChartItemData> myList, "parameter propertyToSortOn",
SortDirection direction)
{
if (direction == SortDirection.Ascending)
var sorted = ChartData.OrderBy(x => x."propertyToSortOn").ToList();
else
var sorted = ChartData.OrderByDescending(x => x."propertyToSortOn").ToList();
}