使用 Framework 2.0 实现 IComparable

发布于 2024-12-04 21:36:57 字数 981 浏览 2 评论 0原文

我正在尝试为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

桃扇骨 2024-12-11 21:36:57

IComparable 是一个接口您在一个您希望能够...比较的类上实现的。 Sort 方法立即知道如何使用 IComparable 接口。因此,如果您想使用 Sort 方法进行排序,您的类必须实现 IComparable 否则,当您在类上调用 CompareTo 时,您会如何期望排序知道你的类如何逻辑排序的算法?

这是关于 IComparable 工作原理的一些背景,但它看起来并不是这里的问题,除非我误读了。看起来您正在尝试:

  1. 用一堆整数填充 OrderedByPopularity
  2. 对整数进行排序。

如果您确实想对列表中的整数进行排序,则只需使用

(i1, i2) => 
{ 
    return i1.CompareTo(i2); 
}

已有的 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 call CompareTo 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:

  1. Fill OrderedByPopularity with a bunch of integers.
  2. Sort the integers.

If you really want to sort the integers in your list, you should just need to use

(i1, i2) => 
{ 
    return i1.CompareTo(i2); 
}

the int (which is the type of element in the List) already has CompareTo on it.

Does this answer the question?

穿越时光隧道 2024-12-11 21:36:57

问题是 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文