Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 229
  • Last Modified:

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
0
tyknight
Asked:
tyknight
1 Solution
 
iboutchkineCommented:
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.
    sc.AddRange(aIn)

    ' Return the new StringCollection to the caller.
    Return sc

End Function


Let me know if you need the code how to sort array
0
 
tyknightAuthor Commented:
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
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now