C#:使用匿名函数排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2122012/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C#: Sorting with anonymous function
提问by JamesBrownIsDead
Let's say I have a list of objects, and I want to sort it by the items DateModified property. Why can't I use a delegate like this? How should I sort these by DateModified if not as shown below:
假设我有一个对象列表,我想按项目 DateModified 属性对其进行排序。为什么我不能使用这样的委托?如果不是如下所示,我应该如何按 DateModified 对这些进行排序:
public string SortByDateModified(List<CartItem> items)
{
items.Sort(new Func<CartItem, CartItem, bool>((itemA, itemB) =>
{
return itemA.DateModified < itemB.DateModified;
}));
}
采纳答案by Samuel Neff
Why not use a lambda expression?
为什么不使用 lambda 表达式?
public string SortByDateModified(List<CartItem> items)
{
items.Sort((a, b) => a.DateModified.CompareTo(b.DateModified));
}
回答by jjxtra
If you don't want to use lambdas or greater than .NET 2.0, use this:
如果您不想使用 lambdas 或大于 .NET 2.0,请使用以下命令:
public string SortByDateModified(List<CartItem> items)
{
items.Sort(delegate(CartItem itemA, CartItem itemB)
{
return itemA.DateModified.CompareTo(itemB.DateModified);
});
}
In my experience, in environments such as Unity, lambdas and even delegates can cause crashes or problems, especially on platforms like iOS. In that case you would want to make your sorter a separate function like so:
根据我的经验,在 Unity 等环境中,lambdas 甚至委托都可能导致崩溃或问题,尤其是在 iOS 等平台上。在这种情况下,您可能希望使分拣机成为一个单独的功能,如下所示:
int SortCartItemFunction(CartItem itemA, CartItem itemB)
{
return itemA.DateModified.CompareTo(itemB.DateModified);
}
Then you could pass it to your sort call like this:
然后你可以像这样将它传递给你的排序调用:
items.Sort(SortCartItemFunction);
回答by Skurmedel
bool
is not useful in such a delegate, usually int
is used because you need 3 values to represent the results of the comparison, less than, equal and greater than. .NET collections usually (if not always) assume -1 means less than, 0 means equal and 1 means greater than.
bool
在这样的委托中没有用,通常int
使用是因为您需要 3 个值来表示比较的结果,小于、等于和大于。.NET 集合通常(如果不总是)假定 -1 表示小于,0 表示等于,1 表示大于。
You would then, in your delegate, have to check if value x is less, equal or greater than value y. An interesting thing to note here is that if you flip the results, for example compare y with x instead you will sort in the opposite direction.
然后,在您的委托中,您必须检查值 x 是否小于、等于或大于值 y。这里要注意的一件有趣的事情是,如果您翻转结果,例如将 y 与 x 进行比较,您将按相反的方向排序。
For the easiest way to sort the dates, check JohnC's answer, or Sam's.
回答by ICR
The Sort
method takes a delegate called Comparison<T>
. You're trying to pass in a Func<int, int, bool>
, which is itself a delegate. There is no conversion between the delegate Func<int, int, bool>
and the delegate Comparison<T>
.
该Sort
方法采用一个名为 的委托Comparison<T>
。您正在尝试传入 a Func<int, int, bool>
,它本身就是一个委托。委托Func<int, int, bool>
和委托之间没有转换Comparison<T>
。
You can, however, use a lambda expression.
但是,您可以使用lambda 表达式。
items.Sort((a, b) => a.DateModified.CompareTo(b.DateModified));
Indeed, you use this very lambda expression and pass it into the Func<int, int, bool>
constructor*. However, there is no need. A lambda expression can be converted into any delegate whos signature matches - that is (a, b) => a.DateModified.CompareTo(b.DateModified)
can be assigned to something typed Func<int, int, int>
or something typed Comparison<T>
. In this case we pass it in to something which expects a Comparison<T>
.
实际上,您使用了这个非常 lambda 表达式并将其传递给Func<int, int, bool>
构造函数*。但是,没有必要。可以将 lambda 表达式转换为任何签名匹配的委托 - 即(a, b) => a.DateModified.CompareTo(b.DateModified)
可以分配给类型Func<int, int, int>
或类型的东西Comparison<T>
。在这种情况下,我们将它传递给期望 a 的东西Comparison<T>
。
*
With one minor adjustment. Sort expectes an integer as a return type. Negative values indicate less than, 0 indicates equal, and positive values indicate greater than.
*
通过一个小的调整。Sort 需要一个整数作为返回类型。负值表示小于,0 表示等于,正值表示大于。