C# 使用委托对列表进行排序时出现问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/230588/
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
Problem sorting lists using delegates
提问by wusher
I am trying to sort a list using delegates but I am getting a signature match error. The compiler says I cannot convert from an 'anonymous method'
我正在尝试使用委托对列表进行排序,但出现签名匹配错误。编译器说我无法从“匿名方法”转换
List<MyType> myList = GetMyList();
myList.Sort( delegate (MyType t1, MyType t2) { return (t1.ID < t2.ID); } );
What am I missing?
我错过了什么?
Here are some references I found and they do it the same way.
这是我找到的一些参考资料,它们以相同的方式进行。
采纳答案by cfeduke
I think you want:
我想你想要:
myList.Sort( delegate (MyType t1, MyType t2)
{ return (t1.ID.CompareTo(t2.ID)); }
);
To sort you need something other than "true/false", you need to know if its equal to, greater than, or less than.
要排序你需要的不是“真/假”,你需要知道它是等于、大于还是小于。
回答by Jeff Yates
The Sort doesn't take a binary predicate, it takes a Comparison<T>
delegate which returns an int
not a bool
.
Sort 不采用二元谓词,它采用Comparison<T>
返回int
not a的委托bool
。
The return values are 0
for when the items are equal, <0
for when the first item is less than the second, and >0
for when the first item is greater than the second.
返回值0
适用于项目相等、<0
第一个项目小于第二个>0
项目以及第一个项目大于第二个项目的情况。
回答by David.Chu.ca
Make sure if your ID property is the default value data type, such as Int or String. If the ID is an object reference type, that object should implement IComparer or IComparer.
确保您的 ID 属性是默认值数据类型,例如 Int 或 String。如果 ID 是对象引用类型,则该对象应实现 IComparer 或 IComparer。
回答by David.Chu.ca
Sorry for previous post. The editor does not take < and > characters, and I did not notice the preview right under the editor. If the ID property is an object type, the object should implement IComparer or IComparer<T>.
抱歉之前的帖子。编辑器不带<和>字符,我没有注意到编辑器下的预览。如果 ID 属性是对象类型,则该对象应实现 IComparer 或 IComparer<T>。
回答by Coderer
In future, if you want to debug problems like this, I'd advocate breaking out the delegate definition from the Sort call, like this:
将来,如果您想调试这样的问题,我建议从 Sort 调用中分离出委托定义,如下所示:
Comparison<MyType> c = delegate(MyType t1, MyType t2){ ... };
myList.Sort(c);
That way, you can see if the problem is in your method call, or in your delegate definition. Some people prefer to leave it this way (with a more descriptive name than "c", obviously) to make the code more readable. I could take it or leave it =-)
这样,您就可以查看问题是出在您的方法调用中,还是出在您的委托定义中。有些人更喜欢保留这种方式(显然,名称比“c”更具描述性)以使代码更具可读性。我可以接受或离开它=-)
回答by David.Chu.ca
The way of obj.Sort(delegate(...)); is dynamic sorting in one place. If you have several places doing the same sorting or you need more flexible sorting, you may consider to create a class implementing IComparer<T>. Here is an example:
obj.Sort(delegate(...)); 的方式 是在一处动态排序。如果你有几个地方做同样的排序,或者你需要更灵活的排序,你可以考虑创建一个实现 IComparer<T> 的类。下面是一个例子:
public class MyTypeComparer : IComparer<MyType>
{
public MyTypeComparer() // default comparer on ID
{ ... }
public MyTypeComparer(bool desc) // default with order specified
public MyTypeComparer(string sort, bool desc) // specified sort and order such as property name, true or false.
{ ... }
public int Compare(MyType a, MyType b) // implement IComparer interface
{ ... } // this is real sorting codes
}
and here is the example to use it:
这是使用它的示例:
List<MyType> myList = GetList();
myList.Sort(new MyTypeComparer());
// myList.Sort(new MyTypeComparer(false));
// myList.Sort(new MyTypeComparer("FirstName", true));