我有一個對象列表,它存儲了我所做的源搜索的搜索結果。在添加到列表中時,我會爲該對象提供有關結果相關性的評分,以便我可以將這些結果推到頂部。IComparable沒有正確排序對象
我的對象實現IComparable
接口,並有一個compareto函數,所有編譯都正確,但是當我對列表(list.sort())進行排序時,這似乎對結果沒有任何影響(較高的評分項目不在底部)
任何人都可以建議我做錯了什麼?
Public Class SearchFeedItem
Implements IComparable
Private _score As Integer = 0
Public Property Score() As Integer
Get
Return _score
End Get
Set(ByVal value As Integer)
_score = value
End Set
End Property
Public Function CompareTo(ByVal obj As Object) As Integer Implements System.IComparable.CompareTo
Dim OtherItem As SearchFeedItem = CType(obj, SearchFeedItem)
If Me.Score < OtherItem.Score Then
Return 1
End If
If Me.Score > OtherItem.Score Then
Return -1
Else
Return 0
End If
End Function
End Class
我已經實現如上述,但仍不能正確排序,新的代碼:公共功能的CompareTo(BYVAL其他作爲SearchFeedItem)作爲整數器具System.IComparable(OF SearchFeedItem).CompareTo 如果Me.Score other.Score然後 返回-1 Else Return 0 End If End Function –
2012-07-20 11:55:04