当 Array.Sort 调用 x.CompareTo(x) 时,IComparer 未返回零
我已经实现了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不太确定
y.ExpirationDate = x.ExpirationDate
。尝试Not too sure about the
y.ExpirationDate = x.ExpirationDate
. Try如果 x 是 y,您可以简单地返回 0。
结束课程
You could simply return 0 if x is y.
End Class