当 Array.Sort 调用 x.CompareTo(x) 时,IComparer 未返回零

发布于 2024-10-30 06:54:58 字数 712 浏览 0 评论 0原文

我已经实现了 IComparer 来对搜索页面上的结果进行排序。有时,在生产中,用户会收到此错误。用于搜索的所有数据(条件、分页、排序)都位于查询字符串中,并且我正在使用 ELMAH 库,因此我可以在错误的电子邮件报告中查看详细信息。如果我将出现错误的用户的查询字符串复制到我的浏览器中,则该页面可以正常工作。所以这显然是不确定的。

这是我的比较器:

Public Class ReverseDateComparer
    Implements IComparer(Of Promotion)

    Public Function Compare(ByVal x As Promotion, ByVal y As Promotion) As Integer Implements System.Collections.Generic.IComparer(Of Promotion).Compare
        If y.ExpirationDate = x.ExpirationDate Then
            Return x.PlainTitle.CompareTo(y.PlainTitle)
        Else
            Return y.ExpirationDate.CompareTo(x.ExpirationDate)
        End If
    End Function
End Class

基本上,按到期日期降序排序,然后按标题升序排序。有什么明显的问题吗?

I've implemented an IComparer to sort results on a search page. Sometimes, in production, users are getting this error. All the data for the search (criteria, paging, sorting) is on the querystring, and I'm using the ELMAH library, so I can see the details in the email report of the error. If I copy the querystring from the user who got the error into my browser, the page works just fine. So it's sort of non-deterministic, apparently.

This is my Comparer:

Public Class ReverseDateComparer
    Implements IComparer(Of Promotion)

    Public Function Compare(ByVal x As Promotion, ByVal y As Promotion) As Integer Implements System.Collections.Generic.IComparer(Of Promotion).Compare
        If y.ExpirationDate = x.ExpirationDate Then
            Return x.PlainTitle.CompareTo(y.PlainTitle)
        Else
            Return y.ExpirationDate.CompareTo(x.ExpirationDate)
        End If
    End Function
End Class

So basically, sort by expiration date descending, then by title ascending. Is there anything obviously wrong with it?

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

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

发布评论

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

评论(2

疯到世界奔溃 2024-11-06 06:54:58

不太确定 y.ExpirationDate = x.ExpirationDate。尝试

Public Class ReverseDateComparer
    Implements IComparer(Of Promotion)

    Public Function Compare(x As Promotion, y As Promotion) As Integer
        Dim c As Integer = x.ExpirationDate.CompareTo(y.ExpirationDate)
        If c = 0 Then
            Return x.PlainTitle.CompareTo(y.PlainTitle)
        Else
            Return c
        End If
    End Function
End Class

Not too sure about the y.ExpirationDate = x.ExpirationDate. Try

Public Class ReverseDateComparer
    Implements IComparer(Of Promotion)

    Public Function Compare(x As Promotion, y As Promotion) As Integer
        Dim c As Integer = x.ExpirationDate.CompareTo(y.ExpirationDate)
        If c = 0 Then
            Return x.PlainTitle.CompareTo(y.PlainTitle)
        Else
            Return c
        End If
    End Function
End Class
℡寂寞咖啡 2024-11-06 06:54:58

如果 x 是 y,您可以简单地返回 0。

Public Class ReverseDateComparer
Implements IComparer(Of Promotion)

Public Function Compare(ByVal x As Promotion, ByVal y As Promotion) As Integer Implements System.Collections.Generic.IComparer(Of Promotion).Compare
    If x is y Then
        Return 0
    ElseIf y.ExpirationDate = x.ExpirationDate Then
        Return x.PlainTitle.CompareTo(y.PlainTitle)
    Else
        Return y.ExpirationDate.CompareTo(x.ExpirationDate)
    End If
End Function

结束课程

You could simply return 0 if x is y.

Public Class ReverseDateComparer
Implements IComparer(Of Promotion)

Public Function Compare(ByVal x As Promotion, ByVal y As Promotion) As Integer Implements System.Collections.Generic.IComparer(Of Promotion).Compare
    If x is y Then
        Return 0
    ElseIf y.ExpirationDate = x.ExpirationDate Then
        Return x.PlainTitle.CompareTo(y.PlainTitle)
    Else
        Return y.ExpirationDate.CompareTo(x.ExpirationDate)
    End If
End Function

End Class

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