排序列表< T>使用字符串,不Linq的 [英] Sort List<T> using string without Linq
问题描述
有没有一种方法来排序列表< T>
使用字符串如名称递减
(同为 DataTable.DefaultView.Sort
),而不是LINQ的?
我试图取代数据表
与列表
,我需要做的这是与旧代码的兼容。
SOLUTION
使用V4Vendetta的代码,我能创造这个扩展方法,测试似乎表明它的工作。
公共静态无效SortByString< T>(名单< T>列表,串sortString)
{
如果(sortString = = NULL)回报;
名单,LT;字符串> SortGroups = sortString.Split(,)了ToList();
的for(int i = SortGroups.Count - 1; I> = 0; I - )//排序从最后一组第一个
{
串tempColumn = SortGroups [I] .Trim()拆分('')[0];
布尔isAsc = SortGroups [I] .Trim()斯普利特('')。长度和GT。 1? SortGroups [I] .Trim()。斯普利特('')[1] .ToLower()==ASC?真:假的:真实的;
的PropertyInfo propInfo = typeof运算(T).GetProperty(tempColumn);
如果(propInfo == NULL)//如果为null检查,以确保它不只是一个外壳的问题。
{
的foreach(typeof运算中PI的PropertyInfo(T).GetProperties())
{
如果(pi.Name.ToLower()== tempColumn.ToLower())
{
tempColumn = pi.Name;
propInfo = typeof运算(T).GetProperty(tempColumn);
中断;
}
}
}
如果(propInfo!= NULL)
{
比较和LT; T>比较=委托(T A,T B)
{
对象值a = isAsc? propInfo.GetValue(一,空):propInfo.GetValue(B,NULL);
对象valueB,则= isAsc? propInfo.GetValue(B,NULL):propInfo.GetValue(一,NULL);
返回值a是IComparable的? ((IComparable的)值a).CompareTo(valueB,则):0;
};
list.Sort(比较);
}其他{
抛出新IndexOutOfRangeException(物业:'+ tempColumn +',不中不存在'的typeof(T)的ToString()+');
}
}
}
我曾试图在这些线路上的东西,也许你需要即兴它CIT您的需求。
私人列表<员工> CreateSortList< T>(
的IEnumerable<员工>数据源,
串字段名,SortDirection sortDirection)
{
名单<员工> returnList =新的List<员工>();
returnList.AddRange(数据源);从字段名通过
System.Reflection.PropertyInfo propInfo = typeof运算(T).GetProperty(字段名)
//获取财产;
比较和LT;员工>比较=代表(员工A,员工B)
{
布尔ASC = sortDirection == SortDirection.Ascending;
对象值a = ASC? propInfo.GetValue(一,空):propInfo.GetValue(B,NULL);
对象valueB,则= ASC? propInfo.GetValue(B,NULL):propInfo.GetValue(一,NULL);
//比较项目
返回值a是IComparable的? ((IComparable的)值a).CompareTo(valueB,则):0;
};
returnList.Sort(比较);
返回returnList;
}
您需要在适当的排序方向和这将是财产的字段名传递类(在我的情况下,员工)的
希望这有助于
Is there a way to sort a List<T>
using a string like "Name desc"
(same as DataTable.DefaultView.Sort
) rather then Linq?
I'm trying to replace DataTables
with Lists
and I need it to do this to be compatible with old code.
SOLUTION
using V4Vendetta's code I was able to create this extension method, tests seem to show it working.
public static void SortByString<T>(this List<T> list, string sortString)
{
if (sortString == null) return;
List<string> SortGroups = sortString.Split(',').ToList();
for (int i = SortGroups.Count - 1; i >= 0; i--)// sort from the last group first
{
string tempColumn = SortGroups[i].Trim().Split(' ')[0];
bool isAsc = SortGroups[i].Trim().Split(' ').Length > 1 ? SortGroups[i].Trim().Split(' ')[1].ToLower() == "asc" ? true : false : true;
PropertyInfo propInfo = typeof(T).GetProperty(tempColumn);
if (propInfo == null) // if null check to make sure its not just a casing issue.
{
foreach (PropertyInfo pi in typeof(T).GetProperties())
{
if(pi.Name.ToLower() == tempColumn.ToLower())
{
tempColumn = pi.Name;
propInfo = typeof(T).GetProperty(tempColumn);
break;
}
}
}
if (propInfo != null)
{
Comparison<T> compare = delegate(T a, T b)
{
object valueA = isAsc ? propInfo.GetValue(a, null) : propInfo.GetValue(b, null);
object valueB = isAsc ? propInfo.GetValue(b, null) : propInfo.GetValue(a, null);
return valueA is IComparable ? ((IComparable)valueA).CompareTo(valueB) : 0;
};
list.Sort(compare);
}else{
throw new IndexOutOfRangeException("Property: '" + tempColumn + "', does not exist in '" + typeof(T).ToString() + "'");
}
}
}
I had tried something on these lines maybe you need to improvise it a cit for your need
private List<Employee> CreateSortList<T>(
IEnumerable<Employee> dataSource,
string fieldName, SortDirection sortDirection)
{
List<Employee> returnList = new List<Employee>();
returnList.AddRange(dataSource);
// get property from field name passed
System.Reflection.PropertyInfo propInfo = typeof(T).GetProperty(fieldName);
Comparison<Employee> compare = delegate(Employee a, Employee b)
{
bool asc = sortDirection == SortDirection.Ascending;
object valueA = asc ? propInfo.GetValue(a, null) : propInfo.GetValue(b, null);
object valueB = asc ? propInfo.GetValue(b, null) : propInfo.GetValue(a, null);
//comparing the items
return valueA is IComparable ? ((IComparable)valueA).CompareTo(valueB) : 0;
};
returnList.Sort(compare);
return returnList;
}
You need to pass in the appropriate sort direction and the fieldname which would be property of the class (in my case Employee)
Hope this helps
这篇关于排序列表< T>使用字符串,不Linq的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!