I have a and mini
class and a List<T>
thereof.
List<mini> result;
public class mini
{
public long SN;
public int PlayTime;
public string Date;
public int Score;
}
//The value is the Name of SN, PlayTime, Date, Score
string sortColumn;
string sortColumnDir; //asc, desc value
The caller can specify a member name to sort on, and I try to sort on the result value:
if("SN" == sortColumn)
var sortresult = result.OrderBy(c => c.SN).ToList<miniCompletion>();
else if("PlayTime" == Date)
var sortresult = result.OrderBy(c => c.PlayTime).ToList<miniCompletion>();
else if("PlayTime" == sortColumn)
var sortresult = result.OrderBy(c => c.PlayTime).ToList<miniCompletion>();
else if("Score" == sortColumn)
var sortresult = result.OrderBy(c => c.Score).ToList<miniCompletion>();
But this code is too inefficient, because it involves a lot of copy-pasting nearly duplicate code. And for sorting descendingly, the code doubles in size.
So I tried:
var sortresult = result.OrderBy(c => c.GetType().GetMember(sortColumn)[0].Name).ToList();
But the sort failed.