I\'m reviewing a piece of code I wrote not too long ago, and I just hate the way I handled the sorting - I\'m wondering if anyone might be able to show me a better way.
It seems to me that there are two immediate improvements we can make:
the logic that uses frm.SortAscending to decide between OrderBy and OrderByDesccending is duplicated in every case, and can be pulled out to after the switch if the cases are changed to do nothing more than establishing the sort key and putting it in a Func
that still leaves the switch itself of course - and this can be replaced by a static map (in a Dictionary, say) from PortfolioSheetMapping to a Func taking a Holding and returning the sort key. eg