My problem is repetitive code: a not so DRY switch statement.
So I have a table with 12 columns that can be ordered by descending or ascending on click. My current solution is the use of a switch statement that checks which column was clicked.
The sortable properties:
This is the page where if a user clicks on a head the table gets ordered:
The SortByColumn
property comes in a string. SortAscending
boolean comes in from a @Html.CheckBoxFor
.
You see where this is going? I have 12 columns that can be ordered, so this switch can get very lengthy and unmaintainable. So my question is, is it possible to refactor this with reflection or in some other way?
The OrderBy function works by letting you return the property it should sort on, it will be called foreach item in the list.
Instead of hardcoding it, we can use reflection instead: