-
Notifications
You must be signed in to change notification settings - Fork 3
/
M_omCollectionFunctions.def
49 lines (40 loc) · 1.18 KB
/
M_omCollectionFunctions.def
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
Option Compare Database
Option Explicit
Public Sub SortCollectionTest()
Dim d As New Collection
Dim i As omKeyValue
Set i = New omKeyValue
i.Key = 1
i.Value = "hallo"
d.Add i
Set i = New omKeyValue
i.Key = -1
i.Value = "test"
d.Add i
SortCollection d
End Sub
Public Function SortCollection(data As Collection, Optional asNumeric As Boolean = True) As Boolean
Dim vItm As Variant
Dim i As Long, j As Long
Dim vTemp As Object
For i = 1 To data.Count - 1
For j = i + 1 To data.Count
If CompareKeys(data(i).Key, data(j).Key, asNumeric) Then
'store the lesser item
Set vTemp = data(j)
'remove the lesser item
data.Remove j
're-add the lesser item before the greater Item
data.Add vTemp, , i
SortCollection = True
End If
Next j
Next i
End Function
Public Function CompareKeys(x As Variant, y As Variant, Optional asNumeric As Boolean = True) As Boolean
If asNumeric Then
CompareKeys = (CDbl(x) > CDbl(y))
Else
CompareKeys = (x > y)
End If
End Function