Creating a needSwap function.

If you want to sort a custom object, you should create two public functions. toString() which will return the value to use for sorting on, and needSwap() which will tell the sort function whether or not the current object is in the wrong position relative to the object it is being compared against. Here are these functions for our cMyClass object. These can be copied directly in to your class with only minor amendments.

Public Function needSwap(cc As cMyClass, e As eSort) As Boolean

' this can be used from a sorting alogirthm

Select Case e

Case eSortAscending

needSwap = LCase(toString) > LCase(cc.toString)

Case eSortDescending

needSwap = LCase(toString) < LCase(cc.toString)

Case Else

needSwap = False

End Select

End Function

Public Function toString() As String

toString = pName

End Function

The sort Collection procedure

This is a very simple kind of bubble sort. Note that it calls the needSwap() function and the toString() function that you will have created in your class.

Option Explicit

' sort a collection

Public Enum eSort

eSortNone

eSortAscending

eSortDescending

End Enum

Function SortColl(ByRef coll As Collection, Optional eorder As Long = eSortAscending) As Long

Dim ita As Long, itb As Long

Dim va As Object, vb As Object, bSwap As Boolean

Dim x As Object, y As Object

For ita = 1 To coll.Count - 1

For itb = ita + 1 To coll.Count

Set x = coll(ita)

Set y = coll(itb)

bSwap = x.needSwap(y, eorder)

If bSwap Then

With coll

Set va = coll(ita)

Set vb = coll(itb)

.add va, x.toString , itb

.add vb, y.toString , ita

.Remove ita + 1

.Remove itb + 1

End With

End If

Next

Next

Sorting the heirarchy

As discussed in Getting started with recursion , cycling through parents and children is straightforward. With this technique so is sorting the children within each parent. In this case we will sort in descending order. Here is the updated test to print out the tree structure, but also to sort it first.