Sorting a collection

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,832
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
This code sorts a collection:

Code:
Public Function SortCollection(ByRef coll As Collection) As Collection

    Dim i As Long, j As Long
    
    Dim k As Variant
    
    For i = 1 To coll.Count - 1
        
        For j = i + 1 To coll.Count
            
            If UCase(String:=coll.Item(i)) > UCase(String:=coll.Item(j)) Then
               
               k = coll.Item(j)
               
               With coll
               
                    .Remove j
               
                    .Add Item:=k, _
                         Key:=k, _
                         Before:=i
            
                End With
                
            End If
        
        Next j
    
    Next i
    
    Set SortCollection = coll

End Function

It works if I test it as follows:

Code:
Sub Test()

    Dim coll As Collection
    Set coll = New Collection
    
    coll.Add "a"
    coll.Add "1"
    
    Set coll = SortCollection(coll)
    
End Sub

but fails if the "1" is not a string, ie this fails:

Code:
Sub Test()

    Dim coll As Collection
    Set coll = New Collection
    
    coll.Add "a"
    coll.Add 1
    
    Set coll = SortCollection(coll)
    
End Sub

Can someone please tell me why is that?

Thanks
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
You can refer to items in a collection either by their key or by their index position. If you use a numeric value for key, it is assumed to be the index so in this case you are trying to add key 1 when there is already an item at position 1.
 
Upvote 0
Solution
You can refer to items in a collection either by their key or by their index position. If you use a numeric value for key, it is assumed to be the index so in this case you are trying to add key 1 when there is already an item at position 1.
Thanks.

But why does the following work, taken from here:

Code:
https://excelmacromastery.com/excel-vba-collections/

(slightly adpated, in that I changed the Sub to a function):

Code:
Option Explicit

Sub TestSort()

    Dim coll As New Collection
    coll.Add "a"
    coll.Add 1
   
    Set coll = QuickSort(coll, 1, coll.Count)
   
End Sub

Function QuickSort(coll As Collection, first As Long, last As Long) As Collection
 
  Dim vCentreVal As Variant, vTemp As Variant
 
  Dim lTempLow As Long
  Dim lTempHi As Long
  lTempLow = first
  lTempHi = last
 
  vCentreVal = coll((first + last) \ 2)
  Do While lTempLow <= lTempHi
 
    Do While coll(lTempLow) < vCentreVal And lTempLow < last
      lTempLow = lTempLow + 1
    Loop
   
    Do While vCentreVal < coll(lTempHi) And lTempHi > first
      lTempHi = lTempHi - 1
    Loop
   
    If lTempLow <= lTempHi Then
   
      ' Swap values
      vTemp = coll(lTempLow)
     
      coll.Add coll(lTempHi), After:=lTempLow
      coll.Remove lTempLow
     
      coll.Add vTemp, Before:=lTempHi
      coll.Remove lTempHi + 1
     
      ' Move to next positions
      lTempLow = lTempLow + 1
      lTempHi = lTempHi - 1
     
    End If
   
  Loop
 
  If first < lTempHi Then QuickSort coll, first, lTempHi
  If lTempLow < last Then QuickSort coll, lTempLow, last
 
  Set QuickSort = coll
 
End Function
 
Upvote 0
Because it doesn't attempt to assign any keys.
 
Upvote 0
Because it doesn't attempt to assign any keys.
Great, thanks for clarifying things.

So to make the first one "work", I could've just removed the Key:=k line and not have to convert the 1 to a string?

Between the two functions, is there a preference?
 
Upvote 0
Yep.

Between the two functions, is there a preference?

Not for me. They're basically the same.
 
Upvote 0
But I do recommend you get out of the habit of doing this:

Code:
Dim coll As New Collection

even in a test proc.
 
Upvote 0
But I do recommend you get out of the habit of doing this:

Code:
Dim coll As New Collection

even in a test proc.
Actually I NEVER do that in my "real code", :)

I use the two line method:

Code:
Dim Coll As Colllection
Set Coll = New Collection

Dim coll As New Collection was copied from the webpage.
 
Upvote 0

Forum statistics

Threads
1,214,614
Messages
6,120,530
Members
448,969
Latest member
mirek8991

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top