这个自定义比较功能有什么问题

发布于 2024-08-08 23:09:14 字数 813 浏览 4 评论 0 原文

我试图调试一个问题并遇到了这个问题。也许有人可以向我解释一下。这是有问题的代码

public int Compare(CustomClass rt1, CustomClass rt2)
{
     if (rt1 == null & rt2 == null)
                return 0;
     if (rt1 == null)
                return -1;
     if (rt2 == null)
                return 1;
     if (rt1.yPos < rt2.yPos)
                return -1;
     if (rt1.yPos == rt2.yPos)
     {
                if (rt1.xPos < rt2.xPos)
                    return -1;
                if (rt1.xPos == rt2.xPos)
                    return 0;
     }
     return 1;
}

我得到的错误是:当 Array.Sort 调用 x 时,IComparer(或其依赖的 IComparable 方法)没有返回零。比较 (x)。

更有趣的是,如果我在调试模式下从 VS 运行它,就不会出现错误。仅当我将其置于发布模式并点击“启动而不调试”时。有人知道为什么会发生这种情况吗?我通过添加“if(rt1 == rt2) return 0;”解决了该问题行到函数的开头,但真的很想知道发生了什么。

额外信息: 是的,这实现了 IComparer 类

I was trying to debug a problem and ran into this issue. Maybe somebody can explain it to me. This is the code in question

public int Compare(CustomClass rt1, CustomClass rt2)
{
     if (rt1 == null & rt2 == null)
                return 0;
     if (rt1 == null)
                return -1;
     if (rt2 == null)
                return 1;
     if (rt1.yPos < rt2.yPos)
                return -1;
     if (rt1.yPos == rt2.yPos)
     {
                if (rt1.xPos < rt2.xPos)
                    return -1;
                if (rt1.xPos == rt2.xPos)
                    return 0;
     }
     return 1;
}

The error I was getting was: IComparer (or the IComparable methods it relies upon) did not return zero when Array.Sort called x. CompareTo(x).

To make it even more interesting, the error would not occur if I ran it from VS in debug mode. Only if I put it in release mode AND hit "start without debugging". Anybody have any ideas why this would happen? I fixed the problem by adding an "if(rt1 == rt2) return 0;" line to the beginning of the function, but would really like to know what is going on.

Extra info:
Yes, this implements the IComparer class

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

你好,陌生人 2024-08-15 23:09:14

你缺少一个&amp;)(这是一个错字吗?)

if (rt1 == null & rt2 == null) // oops!
if (rt1 == null && rt2 == null) // like this....

your missing an amperstand (&) (is this a typo?)

if (rt1 == null & rt2 == null) // oops!
if (rt1 == null && rt2 == null) // like this....
£冰雨忧蓝° 2024-08-15 23:09:14

有时排序算法最终会将对象与其自身进行比较。当这种情况发生时,它触发了代码:

if (rt1 == null)
    return -1;

正是这个导致了错误。您必须确保涵盖所有案例。

如果这些 x,y 值代表点,您可能需要查看有关对它们进行排序的文章: http://www.c-sharpcorner.com/UploadFile/mgold/SortedDictionarySample06202007142815PM/SortedDictionarySample.aspx

Sometimes a sort algorithm will end up comparing an object to itself. When this happened, it triggered the code:

if (rt1 == null)
    return -1;

It was this that caused the error. You've got to be sure that you have all cases covered.

And if those x,y values represent points, you might want to check this article on sorting them: http://www.c-sharpcorner.com/UploadFile/mgold/SortedDictionarySample06202007142815PM/SortedDictionarySample.aspx

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