gpt4 book ai didi

c# - 在不对字段或方向进行硬编码的情况下订购列表

转载 作者:行者123 更新时间:2023-11-30 19:27:46 25 4
gpt4 key购买 nike

我有一个 ObservableCollection,我想对它进行排序,但我想创建一个新的排序副本。

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

我有一个 NSSortDescription 数组,它的工作方式有点像 SortDescription。有一个包含属性名称的 string,但方向由 bool 指定(true = ascending)。数组中的第一个值应该是主要排序字段,当该字段中的值匹配时,应该使用第二个排序描述符,依此类推。

例子:

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

关于如何实现这一点有什么建议吗?

最佳答案

更新:将 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);
}
}
}

关于c# - 在不对字段或方向进行硬编码的情况下订购列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17912479/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com