1

I have a list of objects that store search results for a feed search I have made. While adding to the list I give the object a score on how relevant the result is so that I can push these results to the top.

My object implements the IComparable interface and has a compareto function and all compiles correctly but when I sort the list (list.sort()) this doesn't seem to have any effect on the results (the higher scored items are not at the top of the bottom)

Can anyone advise what I am doing wrong?

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
1
  • I have implemented as above but still not sorting correctly, new code: Public Function CompareTo(ByVal other As SearchFeedItem) As Integer Implements System.IComparable(Of SearchFeedItem).CompareTo If Me.Score < other.Score Then Return 1 End If If Me.Score > other.Score Then Return -1 Else Return 0 End If End Function Commented Jul 20, 2012 at 11:55

2 Answers 2

1

If you want the higher scored items at the bottom, it should be

 Public Function CompareTo(obj As Object) As Integer Implements IComparable.CompareTo

    If obj Is Nothing Then Return 1
    If Me.Score > DirectCast(obj, SortableObject).Score Then Return 1
    If Me.Score < DirectCast(obj, SortableObject).Score Then Return -1

    Return 0

    End Function

Here's a quick example of an object implementing IComparable which sorts from low to high.

Module Module1

    Sub Main()
        Dim sortableObjects = New List(Of SortableObject) From
                               {New SortableObject With {.Score = 12},
                               New SortableObject With {.Score = 5},
                               New SortableObject With {.Score = 120},
                               New SortableObject With {.Score = 99}}

        sortableObjects.Sort() // 5, 9, 99, 120
    End Sub
End Module

Public Class SortableObject : Implements IComparable
    Public Property Score As Integer

    Public Function CompareTo(obj As Object) As Integer Implements IComparable.CompareTo

    If obj Is Nothing Then Return 1
    If Me.Score > DirectCast(obj, SortableObject).Score Then Return 1
    If Me.Score < DirectCast(obj, SortableObject).Score Then Return -1

    Return 0
    // Edit: Or as Konrad mentioned,  Return (Me.Score.CompareTo(DirectCast(obj, SortableObject).Score)). This sorts the items in ascending order.

    End Function
End Class
Sign up to request clarification or add additional context in comments.

Comments

0

Apart from what Hans said in the comment (you need to implement the generic interface!), there are two things about the code which make me cringe:

  1. Use DirectCast instead of CType. The latter is a general-purpose cast that doesn’t show very well what’s actually done here. DirectCast only allows certain casts to happen, notably no conversions. It’s appropriate here since you don’t convert.

  2. The whole CompareTo method can be shortened to a single line:

    Return Score.CompareTo(OtherItem.Score)
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.