-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodCollections.bas
More file actions
54 lines (46 loc) · 1.37 KB
/
Copy pathmodCollections.bas
File metadata and controls
54 lines (46 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
Attribute VB_Name = "modCollections"
Option Compare Database
Option Explicit
'Sorts the given collection using the Arrays.MergeSort algorithm.
' O(n log(n)) time
' O(n) space
Public Sub sort(col As Collection, Optional ByRef c As IVariantComparator)
Dim a() As Variant
Dim b() As Variant
a = Collections.ToArray(col)
Arrays.sort a(), c
Set col = Collections.FromArray(a())
End Sub
'Returns an array which exactly matches this collection.
' Note: This function is not safe for concurrent modification.
Public Function ToArray(col As Collection) As Variant
Dim a() As Variant
ReDim a(0 To col.Count)
Dim i As Long
For i = 0 To col.Count - 1
a(i) = col(i + 1)
Next i
ToArray = a()
End Function
'Returns a Collection which exactly matches the given Array
' Note: This function is not safe for concurrent modification.
Public Function FromArray(a() As Variant) As Collection
Dim col As Collection
Set col = New Collection
Dim element As Variant
For Each element In a
col.Add element
Next element
Set FromArray = col
End Function
Sub testSort()
Dim myCollection As New Collection
Dim colSorted As New Collection
Dim colItem As Variant
myCollection.Add ("Tables")
myCollection.Add ("Queries")
myCollection.Add ("Forms")
myCollection.Add ("Reports")
myCollection.Add ("Modules")
Collections.sort myCollection
End Sub