订购列表而不对字段或方向进行硬编码

时间:2013-07-28 20:20:11

标签: c# .net

我有一个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 个答案:

答案 0 :(得分: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);
        }
    }
}

答案 1 :(得分: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();

答案 2 :(得分: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);
            }

        }
    }
}

答案 3 :(得分:1)

我曾写过以下扩展方法,这些方法基本上具有OrderByThenBy的效果,具体取决于源是否已经订购:

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变量,因为迭代变量在执行查询时会发生变化 - 有关详细信息,请参阅this question


可能的问题是将对每个项目执行反射操作。最好事先为每个属性构建一个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();

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