1

我已经实现了一个 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

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

4

2 回答 2

2

不太确定 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
于 2011-04-04T17:44:29.533 回答
0

如果 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

结束类

于 2011-04-04T17:46:54.457 回答