?
Solved

How to sort items in a collection?

Posted on 2003-03-18
2
Medium Priority
?
228 Views
Last Modified: 2012-06-27
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
Comment
Question by:tyknight
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 28

Accepted Solution

by:
iboutchkine earned 150 total points
ID: 8166284
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
 

Author Comment

by:tyknight
ID: 8171564
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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question