【发布时间】:2014-04-15 23:43:56
【问题描述】:
假设我有这样的课程
public class model
{
public int id{get;set;}
public string name{get;set;}
public string department{get;set;}
}
我有一个模型类型列表
List<model> modelList = List<model>();
如何按列名和排序方向对模型列表进行排序?
我尝试过的方法:
public List<model> sortModelList(string columnName, SortDirection direction)
{
//Method 1:
//The below code was unable to sort by column and unable to set the sort direction
return modelList.Sort();
//Method 2:
//The below code was unable to sort by the columnName parameter and unable to set the sort direction
return modelList.OrderBy(a=>a.name)
//What I can do in order to sort the list by "columnName" parameter and set the sort direction? (Ascending / Descending)
}
【问题讨论】:
-
简单的答案是您需要一个客户比较功能。我敢肯定,很快,有人会告诉你怎么做。
-
看这个页面的帖子:stackoverflow.com/questions/41244/…