Sorting a Custom Collection Class
I’ve been following Dick’s VBHelpers Build series (1, 2, 3) and his last post reminded me that, from time to time, I need to sort a collection of items in-memory.
I don’t have to sort all that often, so my approach has changed over time. I’ve kind of settled on the following.
Let’s say I have a People collection that contains Person items.
In my Person class I’ve written a method (Function) called CompareTo. It works a lot like VBA’s StrComp, returning -1, 0 or +1 depending on whether Item 1 is less than or greater than Item 2.
I’d use it against two person items:
Dim i As Long
If Me.LastName = per.LastName Then
If Me.FirstName = per.FirstName Then
i = 0
ElseIf Me.FirstName < per.FirstName Then
i = -1
Else
i = 1
End If
ElseIf Me.LastName < per.LastName Then
i = -1
Else
i = 1
End If
CompareTo = i
End Function
In my People collection class, I’ve created a method called Sort that returns itself in sorted order.
It’s an Insertion Sort that I converted from Wikipedia’s article into VBA. Notice how it uses the CompareTo method for deciding on item placement.
Dim i As Long, j As Long, k As Long, bln As Boolean
Dim lngCount As Long, arr() As Long, ppl As People
lngCount = Me.Count
If lngCount > 0 Then
ReDim arr(0 To lngCount – 1)
For i = 0 To lngCount – 1: arr(i) = i + 1: Next
For i = 1 To lngCount – 1
k = arr(i)
j = i – 1
bln = False
Do
If Me(arr(j)).CompareTo(Me(k)) > 0 Then
arr(j + 1) = arr(j)
j = j – 1
If j < 0 Then bln = True
Else
bln = True
End If
Loop Until bln
arr(j + 1) = k
Next
End If
Set ppl = New People
For i = 0 To lngCount – 1: ppl.Add Me(arr(i)): Next
Set Sort = ppl
End Function
Now I get to use the above in my main code routines:
Dim ppl As People, per As Person
Set ppl = New People
ppl.FillFromSheet ActiveSheet
For Each per In ppl.Sort
Debug.Print per.FirstName; vbTab; per.LastName; vbTab; per.Gender.ToString; vbTab; per.City
Next
End Sub
The code above is available for download. It’s an extension of the code I posted a year ago on the same topic (links 1, 2). It also includes the Enum enhancements suggested by Andy Pope way back then.
You can download SortClass.zip
Part of my part-time job schedules world-wide PC-chat conferences weekly, and I announce the time referenced to the East Coast. As