传一个参数,判断使用哪个排序:
List<A> lst = new List<A>
{
new A { Id = "1", Name = "3", Age = 2 },
new A { Id = "2", Name = "1", Age = 3 },
new A { Id = "3", Name = "2", Age = 1 },
};
var switch_on = "idasc";
switch (switch_on)
{
case "idasc":
lst = lst.OrderBy(l => l.Id).ToList(); break;
case "iddesc":
lst = lst.OrderByDescending(l => l.Id).ToList(); break;
case "ageasc":
lst = lst.OrderBy(l => l.Age).ToList(); break;
case "agedesc":
lst = lst.OrderByDescending(l => l.Age).ToList(); break;
default: break;
}