我想通过存储在其中的对象的属性对c#中的列表进行排序。我有这个:
if (sortColumn == "Login")
{
if (sortDir == "ASC")
{
filteredList.Sort((x, y) => string.Compare(x.Login, y.Login, true));
}
else
{
filteredList.Sort((x, y) => string.Compare(y.Login, x.Login, true));
}
}
它工作正常,但我想更通用,以便不必知道要排序的字段。我想过这样的事情:
//With sortColumn = "Login";
if (sortDir == "ASC")
{
filteredList.Sort((x, y) => string.Compare(x.GetType().GetProperty(sortColumn), y.GetType().GetProperty(sortColumn), true));
}
else
{
filteredList.Sort((x, y) => string.Compare(y.GetType().GetProperty(sortColumn), x.GetType().GetProperty(sortColumn), true));
}
显然这不起作用,但这就是我想要的。有可能吗?
感谢。
答案 0 :(得分:3)
反射代码不正确,请看这个
PropertyInfo pi1 = typeof(x).GetProperty(sortColumn);
PropertyInfo pi2 = typeof(y).GetProperty(sortColumn);
//With sortColumn = "Login";
if (sortDir == "ASC")
{
filteredList.Sort((x, y) => string.Compare(pi1.GetValue(x, null), pi2.GetValue(y, null), true));
}
else
{
filteredList.Sort((x, y) => string.Compare(pi2.GetValue(y, null), pi1.GetValue(x, null), true));
}
我认为这对你有用。
答案 1 :(得分:1)
这就是我用于同一问题的方法。
用法如下:mySequence.OrderByPropertyName("Login", SortDirection.Descending)
。
public enum SortDirection
{
Ascending,
Descending
}
public static IOrderedEnumerable<T> OrderByPropertyName<T>
(
this IEnumerable<T> items,
string propertyName,
SortDirection sortDirection = SortDirection.Ascending
)
{
var propInfo = typeof(T).GetProperty(propertyName);
return items.OrderByDirection(x => propInfo.GetValue(x, null), sortDirection);
}
public static IOrderedEnumerable<T> OrderByDirection<T, TKey>
(
this IEnumerable<T> items,
Func<T, TKey> keyExpression,
SortDirection sortDirection = SortDirection.Ascending
)
{
switch (sortDirection)
{
case SortDirection.Ascending:
return items.OrderBy(keyExpression);
case SortDirection.Descending:
return items.OrderByDescending(keyExpression);
}
throw new ArgumentException("Unknown SortDirection: " + sortDirection);
}
答案 2 :(得分:0)
我已经检查了dateTime并且工作正常。
List<DateTime> list = new List<DateTime>();
list.Add(DateTime.Now);
list.Add(DateTime.UtcNow.AddYears(2));
list.Sort((x, y) => (Convert.ToString(x.GetType().GetProperty("DayOfYear").GetValue(x)).CompareTo(Convert.ToString(y.GetType().GetProperty("DayOfYear").GetValue(y)))));
答案 3 :(得分:0)
扩展verdesmarald帖子我将Ascending和Descending分成单独的方法并添加了ThenBy方法:
using System.Collections.Generic;
namespace System.Linq
{
public static class IEnumerableExtensions
{
enum SortDirection
{
Ascending,
Descending
}
public static IOrderedEnumerable<T> OrderBy<T>
(this IEnumerable<T> items,
string propertyName)
{
var propInfo = typeof (T).GetProperty(propertyName);
return items.OrderByDirection(x => propInfo.GetValue(x, null), SortDirection.Ascending);
}
public static IOrderedEnumerable<T> ThenBy<T>
(this IOrderedEnumerable<T> items,
string propertyName)
{
var propInfo = typeof(T).GetProperty(propertyName);
return items.ThenByDirection(x => propInfo.GetValue(x, null), SortDirection.Ascending);
}
public static IOrderedEnumerable<T> OrderByDescending<T>
(this IEnumerable<T> items,
string propertyName)
{
var propInfo = typeof(T).GetProperty(propertyName);
return items.OrderByDirection(x => propInfo.GetValue(x, null), SortDirection.Descending);
}
public static IOrderedEnumerable<T> ThenByDescending<T>
(this IOrderedEnumerable<T> items,
string propertyName)
{
var propInfo = typeof(T).GetProperty(propertyName);
return items.ThenByDirection(x => propInfo.GetValue(x, null), SortDirection.Descending);
}
private static IOrderedEnumerable<T> OrderByDirection<T, TKey>
(this IEnumerable<T> items,
Func<T, TKey> keyExpression,
SortDirection sortDirection)
{
switch (sortDirection)
{
case SortDirection.Ascending:
return items.OrderBy(keyExpression);
case SortDirection.Descending:
return items.OrderByDescending(keyExpression);
}
throw new ArgumentException("Unknown SortDirection: " + sortDirection);
}
private static IOrderedEnumerable<T> ThenByDirection<T, TKey>
(this IOrderedEnumerable<T> items,
Func<T, TKey> keyExpression,
SortDirection sortDirection)
{
switch (sortDirection)
{
case SortDirection.Ascending:
return items.ThenBy(keyExpression);
case SortDirection.Descending:
return items.ThenByDescending(keyExpression);
}
throw new ArgumentException("Unknown SortDirection: " + sortDirection);
}
}
}