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

    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
        End Sub

    End Class
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You can convert collection to array

' Converts a StringCollection to an array of strings.
Public Function StringCollectionToArray(ByRef col As System.Collections.Specialized.StringCollection) As String()

    ' Instantiate an array and sets the array to be equal to the StringCollection's size.
    Dim aOut(col.Count()) As String

    ' Copy the contents of the collection to the array, beginning at the first element of the array.
    col.CopyTo(aOut, 0)

    ' Return the array to the caller.
    Return aOut

End Function

Sort array and then convert it back to collection

' Converts an array of strings to a StringCollection.
Public Function ArrayToStringCollection(ByRef aIn() As String) _
     As System.Collections.Specialized.StringCollection

    ' Instantiate new StringCollection object.
    Dim sc As System.Collections.Specialized.StringCollection =  _
         New System.Collections.Specialized.StringCollection()

    ' Populate the StringCollection with the specified array.

    ' Return the new StringCollection to the caller.
    Return sc

End Function

Let me know if you need the code how to sort array

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tyknightAuthor Commented:

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
                '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
        '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

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

End Function
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.