7

我想通过存储在其中的对象的属性对 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));
}

显然这不起作用,但这就是我想要的。有可能吗?

谢谢。

4

4 回答 4

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));
}

我认为这对你有用。

于 2012-08-31T07:19:33.760 回答
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);
}
于 2012-08-31T07:10:24.940 回答
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)))));
于 2012-08-31T07:21:51.113 回答
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);
        }
    }
}
于 2016-05-31T14:34:54.313 回答