使用 Framework 2.0 实现 IComparable
我正在尝试为 2.0 框架重构以下代码(工作中需要)。下面的代码是根据我试图在上一篇文章中提供的弄清楚如何对数组列表的字典进行排序。这与那次讨论有些偏离。下面是代码:
MultiDimDictList myDicList = new MultiDimDictList();
myDicList.Add("fly", a_fly);
myDicList.Add("img", a_img);
myDicList.Add("bar", a_bar);
myDicList.Add("meter", a_meter);
myDicList.Add("block", a_block);
myDicList.Add("popularity", a_pop);
List<int> OrderedByPopularity = new List<int>();
ArrayList popList = myDicList["popularity"];
for (int i = 0; i < popList.Count; ++i)
{
OrderedByPopularity.Add(i);
}
OrderedByPopularity.Sort((i1, i2) =>
{
return popList[i2].CompareTo(popList[i1]);
});
当我尝试运行上面的代码时,最底层的逻辑给我带来了问题 - 即“CompareTo”无法识别。我已经对此进行了一些阅读,看起来我必须实现 IComparable 才能使其正常工作,但我可以在这里使用一些帮助/建议。我应该在哪里实现 IComparable?另外,编译器还告诉我对我的 .Sort 方法使用“委托”。我对吗,它应该看起来像: .Sort(delegate(int i1, int i2) => ?
感谢您的帮助。
I am trying to refactor the below code for the 2.0 framework (required at work). The below code was kindly provided per a previous post in which I was trying to figure out how to sort a dictionary of arraylists. This is a bit of a tangent from that discussion. Below is the code:
MultiDimDictList myDicList = new MultiDimDictList();
myDicList.Add("fly", a_fly);
myDicList.Add("img", a_img);
myDicList.Add("bar", a_bar);
myDicList.Add("meter", a_meter);
myDicList.Add("block", a_block);
myDicList.Add("popularity", a_pop);
List<int> OrderedByPopularity = new List<int>();
ArrayList popList = myDicList["popularity"];
for (int i = 0; i < popList.Count; ++i)
{
OrderedByPopularity.Add(i);
}
OrderedByPopularity.Sort((i1, i2) =>
{
return popList[i2].CompareTo(popList[i1]);
});
When I try to run the above code, the bottom-most logic is giving me problems - namely, that "CompareTo" is not recognized. I have done some reading up on this and it looks like I have to implement IComparable to get this to work but I could use some help / suggestions here. Where should I be implementing IComparable? Also, the compiler is also telling me to use "delegate" for my .Sort method. Am i right that it should look like: .Sort(delegate(int i1, int i2) => ?
Thanks for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
IComparable 是一个接口您在一个您希望能够...比较的类上实现的。 Sort 方法立即知道如何使用 IComparable 接口。因此,如果您想使用 Sort 方法进行排序,您的类必须实现
IComparable
否则,当您在类上调用CompareTo
时,您会如何期望排序知道你的类如何逻辑排序的算法?这是关于 IComparable 工作原理的一些背景,但它看起来并不是这里的问题,除非我误读了。看起来您正在尝试:
OrderedByPopularity
。如果您确实想对列表中的整数进行排序,则只需使用
已有的
int
(这是List
中的元素类型) 即可CompareTo 就可以了。这能回答问题吗?
IComparable is an interface that you implement on a class that you want to be able to be...comparable. The Sort method knows how to work with the IComparable interface right off the bat. So, it follows that if you want to sort using the Sort method, your class must implement
IComparable
Otherwise, when you callCompareTo
on your class, how would you expect the sorting algorithm to know how your class logically sorts?That is some background on how
IComparable
works, but it doesn't look like it is the problem here unless I am misreading. It looks like you are trying to:OrderedByPopularity
with a bunch of integers.If you really want to sort the integers in your list, you should just need to use
the
int
(which is the type of element in theList
) already hasCompareTo
on it.Does this answer the question?
问题是 ArrayList 返回对象。对象未实现 IComparable。
因此,当您说 popList[i2].CompareTo(blah) 时,您正在调用不存在的对象上的函数。
Problem is that ArrayList returns object. Object does not implement IComparable.
So when you say popList[i2].CompareTo(blah) you are calling a function on object which does not exist.