2

我有一个ObservableCollection我想要排序的文件,但我想创建一个新的排序副本。

有很多关于如何使用漂亮的 lambda 表达式或使用 LINQ 对列表进行排序的示例,但我无法将要排序的字段硬编码为代码。

我有一个数组,NSSortDescription其中的工作有点像SortDescription。有一个string属性名称,但方向由一个booltrue=升序)指定。数组中的第一个值应该是主排序字段,当该字段中的值匹配时,应该使用第二个排序描述符,等等。

例子:

Artist: Bob Marley, Title: No Woman No Cry
Artist: Bob Marley, Title: Could You Be Loved
Artist: Infected Mushroom, Title: Converting Vegetarians
Artist: Bob Marley, Title: One Love
Artist: Chemical Brothers, Title: Do It Again

排序描述符:艺术家降序,标题升序。

结果:

Artist: Infected Mushroom, Title: Converting Vegetarians
Artist: Chemical Brothers, Title: Do It Again
Artist: Bob Marley, Title: Could You Be Loved
Artist: Bob Marley, Title: No Woman No Cry
Artist: Bob Marley, Title: One Love

关于如何做到这一点的任何建议?

4

4 回答 4

4

更新:将 Sort 更改为 OrderBy,因为 Sort 是不稳定的排序算法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.ComponentModel;

namespace PNS
{
    public class SortableList<T> : List<T>
    {
        private string _propertyName;
        private bool _ascending;

        public void Sort(string propertyName, bool ascending)
        {
            //Flip the properties if the parameters are the same
            if (_propertyName == propertyName && _ascending == ascending)
            {
                _ascending = !ascending;
            }
            //Else, new properties are set with the new values
            else
            {
                _propertyName = propertyName;
                _ascending = ascending;
            }

            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
            PropertyDescriptor propertyDesc = properties.Find(propertyName, true);

            // Apply and set the sort, if items to sort
            PropertyComparer<T> pc = new PropertyComparer<T>(propertyDesc, (_ascending) ? ListSortDirection.Ascending : ListSortDirection.Descending);
            //this.Sort(pc); UNSTABLE SORT ALGORITHM
            this.OrderBy(t=>t, pc);
        }
    }

    public class PropertyComparer<T> : System.Collections.Generic.IComparer<T>
    {

        // The following code contains code implemented by Rockford Lhotka:
        // http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnadvnet/html/vbnet01272004.asp

        private PropertyDescriptor _property;
        private ListSortDirection _direction;

        public PropertyComparer(PropertyDescriptor property, ListSortDirection direction)
        {
            _property = property;
            _direction = direction;
        }

        public int Compare(T xWord, T yWord)
        {
            // Get property values
            object xValue = GetPropertyValue(xWord, _property.Name);
            object yValue = GetPropertyValue(yWord, _property.Name);

            // Determine sort order
            if (_direction == ListSortDirection.Ascending)
            {
                return CompareAscending(xValue, yValue);
            }
            else
            {
                return CompareDescending(xValue, yValue);
            }
        }

        public bool Equals(T xWord, T yWord)
        {
            return xWord.Equals(yWord);
        }

        public int GetHashCode(T obj)
        {
            return obj.GetHashCode();
        }

        // Compare two property values of any type
        private int CompareAscending(object xValue, object yValue)
        {
            int result;

            if (xValue == null && yValue != null) return -1;
            if (yValue == null && xValue != null) return 1;
            if (xValue == null && yValue == null) return 0;
            // If values implement IComparer
            if (xValue is IComparable)
            {
                result = ((IComparable)xValue).CompareTo(yValue);
            }
            // If values don't implement IComparer but are equivalent
            else if (xValue.Equals(yValue))
            {
                result = 0;
            }
            // Values don't implement IComparer and are not equivalent, so compare as string values
            else result = xValue.ToString().CompareTo(yValue.ToString());

            // Return result
            return result;
        }

        private int CompareDescending(object xValue, object yValue)
        {
            // Return result adjusted for ascending or descending sort order ie
            // multiplied by 1 for ascending or -1 for descending
            return CompareAscending(xValue, yValue) * -1;
        }

        private object GetPropertyValue(T value, string property)
        {
            // Get property
            PropertyInfo propertyInfo = value.GetType().GetProperty(property);

            // Return value
            return propertyInfo.GetValue(value, null);
        }
    }
}
于 2013-07-28T20:24:33.837 回答
2

OrderBy您可以根据string属性动态创建谓词。

Func<MyType, object> firstSortFunc = null;
Func<MyType, object> secondSortFunc = null;

//these strings would be obtained from your NSSortDescription array
string firstProp = "firstPropertyToSortBy";
string secondProp = "secondPropertyToSortBy";
bool isAscending = true;

//create the predicate once you have the details
//GetProperty gets an object's property based on the string
firstSortFunc = x => x.GetType().GetProperty(firstProp).GetValue(x);
secondSortFunc = x => x.GetType().GetProperty(secondProp).GetValue(x);

List<MyType> ordered = new List<MyType>();

if(isAscending)
   ordered = unordered.OrderBy(firstSortFunc).ThenBy(secondSortFunc).ToList();
else
   ordered = unordered.OrderByDescending(firstSortFunc).ThenBy(secondSortFunc).ToList();
于 2013-07-28T20:40:15.927 回答
2

您可以创建一个名为例如 DynamicProperty 的类,它确实检索请求的值。我确实假设返回的值确实实现了 IComparable ,这不应该是一个太苛刻的限制,因为您确实想要比较这些值。

using System;
using System.Linq;
using System.Reflection;

namespace DynamicSort
{
    class DynamicProperty<T>
    {
        PropertyInfo SortableProperty;

        public DynamicProperty(string propName)
        {
            SortableProperty = typeof(T).GetProperty(propName);
        }

        public IComparable GetPropertyValue(T obj)
        {
            return (IComparable)SortableProperty.GetValue(obj);
        }
    }

    class Program
    {
        class SomeData
        {
            public int X { get; set; }
            public string Name { get; set; }
        }

        static void Main(string[] args)
        {
            SomeData[] data = new SomeData[]
            {
                new SomeData { Name = "ZZZZ", X = -1 },
                new SomeData { Name = "AAAA", X = 5 },
                new SomeData { Name = "BBBB", X = 5 },
                new SomeData { Name = "CCCC", X = 5 }
            };


            var prop1 = new DynamicProperty<SomeData>("X");
            var prop2 = new DynamicProperty<SomeData>("Name");

            var sorted = data.OrderBy(x=> prop1.GetPropertyValue(x))
                             .ThenByDescending( x => prop2.GetPropertyValue(x));

            foreach(var res in sorted)
            {
                Console.WriteLine("{0} X: {1}", res.Name, res.X);
            }

        }
    }
}
于 2013-07-28T21:02:42.823 回答
1

我曾经写过如下的扩展方法,基本上都是OrderByor 或者的效果ThenBy,具体取决于源是否已经下单:

public static class Extensions {
    public static IOrderedEnumerable<TSource> OrderByPreserve<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer, bool descending) {
        var orderedSource = source as IOrderedEnumerable<TSource>;
        if (orderedSource != null) {
            return orderedSource.CreateOrderedEnumerable(keySelector, comparer, descending);
        }
        if (descending) {
            return source.OrderByDescending(keySelector, comparer);
        }
        return source.OrderBy(keySelector, comparer);
    }

    public static IOrderedEnumerable<TSource> OrderByPreserve<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector) {
        return source.OrderByPreserve(keySelector, null, false);
    }

    public static IOrderedEnumerable<TSource> OrderByPreserve<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer) {
        return source.OrderByPreserve(keySelector, comparer, false);
    }

    public static IOrderedEnumerable<TSource> OrderByDescendingPreserve<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector) {
        return source.OrderByPreserve(keySelector, null, true);
    }

    public static IOrderedEnumerable<TSource> OrderByDescendingPreserve<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer) {
        return source.OrderByPreserve(keySelector, comparer, true);
    }
}

该接口与OrderBy/相同(或者您可以作为布尔值OrderByDescending传递)。descending你可以写:

list.OrderByPreserve(x => x.A).OrderByPreserve(x => x.B)

这与以下效果相同:

list.OrderBy(x => x.A).ThenBy(x => x.B)

因此,您可以轻松地将keyboardP的解决方案与任意属性名称列表一起使用:

public static IEnumerable<TSource> OrderByProperties<TSource>(IEnumerable<TSource> source, IEnumerable<string> propertyNames) {
    IEnumerable<TSource> result = source;
    foreach (var propertyName in propertyNames) {
        var localPropertyName = propertyName;
        result = result.OrderByPreserve(x => x.GetType().GetProperty(localPropertyName).GetValue(x, null));
    }
    return result;
}

localPropertyName此处使用该变量是因为在执行查询时迭代变量会发生变化——详情请参阅此问题


一个可能的问题是,将为每个项目执行反射操作。最好事先为每个属性构建一个 LINQ 表达式,以便可以有效地调用它们(此代码需要System.Linq.Expressions命名空间):

public static IEnumerable<TSource> OrderByProperties<TSource>(IEnumerable<TSource> source, IEnumerable<string> propertyNames) {
    IEnumerable<TSource> result = source;
    var sourceType = typeof(TSource);
    foreach (var propertyName in propertyNames) {
        var parameterExpression = Expression.Parameter(sourceType, "x");
        var propertyExpression = Expression.Property(parameterExpression, propertyName);
        var castExpression = Expression.Convert(propertyExpression, typeof(object));
        var lambdaExpression = Expression.Lambda<Func<TSource, object>>(castExpression, new[] { parameterExpression });
        var keySelector = lambdaExpression.Compile();
        result = result.OrderByPreserve(keySelector);
    }
    return result;
}

本质上,这些Expression行所做的是构建表达式x => (object)x.A(其中“A”是当前属性名称),然后将其用作排序键选择器。

示例用法是:

var propertyNames = new List<string>() { "Title", "Artist" };
var sortedList = OrderByProperties(list, propertyNames).ToList();

您只需要添加升序/降序逻辑。

于 2013-07-30T23:23:44.773 回答