如何基于 MyObject 属性创建一个可以接受 sortExpression 的通用方法,如下所示:
void CreateSortedReport(IList<MyObject> list, Expression<Func<MyObject, TSort>> sortExpression, bool ascending = true)
{
//sort the [list] by sortExpression and by direction;
}
所以我可以像这样使用它:
CreateSortedReport(myItems, x=>x.Name);
或
CreateSortedReport(myItems, x=>x.CreateDate);
编辑1: 我之所以要求使用泛型方法是因为有些方法非常相似:
CreateReportSortedByName(myItems) {
return myItems.OrderBy(x=>x.Name);
}
CreateReportSortedByDate(myItems) {
return myItems.OrderBy(x=>x.CreateDate);
}
答案 0 :(得分:1)
假设MyObject
是具体类型,只需在方法签名中添加一个通用参数(<TSort>
):
void CreateReport<TSort>(IList<MyObject> list, Expression<Func<MyObject, TSort>> sortExpression, bool ascending = true)
如果您希望MyObject
成为通用参数,则您的签名将如下所示:
void CreateReport<MyObject, TSort>(IList<MyObject> list, Expression<Func<MyObject, TSort>> sortExpression, bool ascending = true)
答案 1 :(得分:1)
要使用内置排序功能提供就地排序,您需要编写一个实现IComparer<T>
的类。
例如:
class CompareWithDelegate<TOnObject, TSort> : IComparer<TOnObject>
{
Func<TOnObject, TSort> evaluator;
IComparer comparer = Comparer.Default;
bool ascending;
public CompareWithDelegate(Expression<Func<TOnObject, TSort>> expr, bool ascending = true)
{
evaluator = expr.Compile();
this.ascending = ascending;
}
public int Compare(TOnObject left, TOnObject right)
{
var leftVal = evaluator(left);
var rightVal = evaluator(right);
return (ascending ? 1 : -1) * comparer.Compare(leftVal, rightVal);
}
}
然后:
void CreateSortedReport<TSort>(List<MyObject> list, Expression<Func<MyObject, TSort>> sortExpression, bool ascending = true)
{
list.Sort(new CompareWithDelegate<MyObject, TSort>(sortExpression));
list.Dump();
}
请注意,list
必须是List<MyObject>
,而不是IList<MyObject>
。
或者,如果您不需要它就位,您有两种选择:
将签名更改为Func<MyObject, TSort>
:
void CreateSortedReport<TSort>(List<MyObject> list, Func<MyObject, TSort> sortExpression, bool ascending = true)
{
var t =
ascending
? list.OrderBy (sortExpression)
: list.OrderByDescending(sortExpression);
}
或者动态编译表达式:
void CreateSortedReport<TSort>(List<MyObject> list, Expression<Func<MyObject, TSort>> sortExpression, bool ascending = true)
{
var method = sortExpression.Compile();
var t =
ascending
? list.OrderBy (method)
: list.OrderByDescending(method);
}
答案 2 :(得分:1)
您可以基于内置List(of T).Sort(IComparer(of T))。
构建扩展方法public static class SortExtension
{
public static void SortBy<T, TProperty>(this List<T> list, Func<T, TProperty> orderby, bool ascending = true)
{
list.Sort(new InnerComparer<T, TProperty>(orderby, ascending));
}
class InnerComparer<T, TProperty> : IComparer<T>
{
private readonly Func<T, TProperty> _property;
private readonly int _ascending;
public InnerComparer(Func<T, TProperty> property, bool ascending)
{
_property = property;
_ascending = ascending ? 1 : -1;
}
int IComparer<T>.Compare(T x, T y)
{
var p1 = _property(x);
var p2 = _property(y);
return _ascending * Comparer<TProperty>.Default.Compare(p1, p2);
}
}
}
用法:
myObjects.SortBy(o => o.MyProperty);