Link to home
Start Free TrialLog in
Avatar of tyknight
tyknightFlag for Afghanistan

asked on

How to sort items in a collection?

There are two classes: DocCatalog and DocCatalogList.
How can I sort the DocCatalog items in a DocCatalogList by
Rating()?

    Public Class DocCatalog
           :
        Public Function Rating() As Integer
       :
        End Function
    End Class
   
   Public Class DocCatalogList
        Inherits CollectionBase

        Sub New()

        End Sub

        'Add a DocCataLog in this list
        Sub Add(ByVal catalog As DocCatalog)
            ' list is inherts from CollectionBase
            list.Add(catalog)
        End Sub

     :
    End Class
ASKER CERTIFIED SOLUTION
Avatar of iboutchkine
iboutchkine

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tyknight

ASKER

Thanks

I found the following function and modified the code to sort my collection.


Public Function SortItemCollection(col As Collection, strPropertyName, Optional blnCompareNumeric As Boolean = False) As Collection
    Dim colNew As Collection
    Dim objCurrent As Object
    Dim objCompare As Object
    Dim lngCompareIndex As Long
    Dim strCurrent As String
    Dim strCompare As String
    Dim blnGreaterValueFound As Boolean

    'make a copy of the collection, ripping through it one item
    'at a time, adding to new collection in right order...
   
    Set colNew = New Collection
   
    For Each objCurrent In col
   
        'get value of current item...
        strCurrent = CallByName(objCurrent, strPropertyName, VbGet)
       
        'setup for compare loop
        blnGreaterValueFound = False
        lngCompareIndex = 0
       
        For Each objCompare In colNew
            lngCompareIndex = lngCompareIndex + 1
           
            strCompare = CallByName(objCompare, strPropertyName, VbGet)
           
            'optimization - instead of doing this for every iteration, have 2 different loops...
            If blnCompareNumeric = True Then
                'this means we are looking for a numeric sort order...
               
                If Val(strCurrent) < Val(strCompare) Then
                    'found an item in compare collection that is greater...
                    'add it to the new collection...
                    blnGreaterValueFound = True
                    colNew.Add objCurrent, , lngCompareIndex
                    Exit For
                End If
               
            Else
                'this means we are looking for a string sort...
               
                If strCurrent < strCompare Then
                    'found an item in compare collection that is greater...
                    'add it to the new collection...
                    blnGreaterValueFound = True
                    colNew.Add objCurrent, , lngCompareIndex
                    Exit For
                End If
           
            End If
        Next
       
        'if we didn't find something bigger, just add it to the end of the new collection...
        If blnGreaterValueFound = False Then
            colNew.Add objCurrent
        End If
             
    Next

    'return the new collection...
    Set SortItemCollection = colNew
    Set colNew = Nothing

End Function