C# linq sort - 实例化 IComparer 的快速方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1381564/
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# linq sort - quick way of instantiating IComparer
提问by Daniel
When using linq and you have
当使用 linq 并且你有
c.Sort()
Is there any good inline way of defining a Comparison
and/or IComparer
class without actually having to create a separate class?
是否有任何好的内联方式来定义一个Comparison
和/或IComparer
类而实际上不必创建一个单独的类?
采纳答案by ckarras
That's one of the uses of lambda expressions:
这是 lambda 表达式的用途之一:
c.Sort( (x,y) => x.A.CompareTo(y.A))
c.Sort( (x,y) => x.A.CompareTo(y.A))
回答by Jon Skeet
I have a ProjectionComparer
class in MiscUtil, so you can do:
我ProjectionComparer
在MiscUtil有一堂课,所以你可以这样做:
IComparer<Foo> comparer = ProjectionComparer<Foo>.Create(x => x.Name);
c.Sort(comparer);
The code is also in this answer.
代码也在这个答案中。
You can create a Comparison<T>
instance directly with a lambda expression too, but I don't generally like the duplication that involves. Having said which, it often ends up being somewhat shorter...
您也可以Comparison<T>
直接使用 lambda 表达式创建实例,但我通常不喜欢所涉及的重复。话虽如此,它通常最终会更短一些......
EDIT: As noted, as of .NET 4.5, use Comparer<T>.Create
to do the same thing.
编辑:如前所述,从 .NET 4.5 开始,Comparer<T>.Create
用来做同样的事情。
回答by Pavel Minaev
I've no idea what c.Sort()
is in your example, as it can be many things (do you mean List<T>.Sort()
?), but one thing that it sure isn't is LINQ. LINQ doesn't have Sort()
- it has OrderBy()
.
我不知道c.Sort()
你的例子中是什么,因为它可以是很多东西(你的意思是List<T>.Sort()
?),但它肯定不是的一件事是 LINQ。LINQ 没有Sort()
- 它有OrderBy()
.
That said, the latter also works with IComparer
, and there's no way to create an instance of anonymous class implementing the interface "inline", so you'll have to define a class.
也就是说,后者也适用于IComparer
,并且无法创建实现“内联”接口的匿名类的实例,因此您必须定义一个类。
For List<T>.Sort()
, there is an overload which takes Comparison<T>
. Since it's a delegate type, you can use a lambda to provide the function inline:
对于List<T>.Sort()
,有一个需要 的重载Comparison<T>
。由于它是委托类型,您可以使用 lambda 来提供内联函数:
List<int> xs = ...;
xs.Sort((x, y) => y - x); // reverse sort
回答by codymanix
If the objects in the List c already implement IComparable you wont need another one. But if you need custom comparison, you can implement IComparer in a nested class. You also can use a lambda expression to create a Comparison method on the fly:
如果列表 c 中的对象已经实现了 IComparable,则您不需要另一个。但如果需要自定义比较,可以在嵌套类中实现 IComparer。您还可以使用 lambda 表达式动态创建比较方法:
persons.Sort( (person1, person2) => person1.Age.CompareTo( person2.Age ) );
persons.Sort( (person1, person2) => person1.Age.CompareTo( person2.Age ) );
回答by illegal-immigrant
Jon's answer is great but can be a little bit out of date,
with release of .NET 4.5 we now (finally!) have this awesome method Comparer<T>.Create
Jon 的回答很好,但可能有点过时,随着 .NET 4.5 的发布,我们现在(终于!)有了这个很棒的方法Comparer<T>.Create
items.Sort((x, y) => x.Value.CompareTo(y.Value)); //sorting List<T>
items.OrderBy(x => x, Comparer<Item>.Create((x, y) => x.Value.CompareTo(y.Value))); //sorting IEnumerable<T>
Assuming Item
is defined something like:
假设Item
定义如下:
class Item
{
public readonly int Key;
public readonly string Value;
public Item(int key, string value)
{
Key = key;
Value = value;
}
}