forked from Vitosh/VBA_personal
-
Notifications
You must be signed in to change notification settings - Fork 5
/
SortArraySortList.vb
85 lines (58 loc) · 2.28 KB
/
SortArraySortList.vb
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
'sort array arraysort array sort sortlist listsort sortlist bubblesort bubble sort
Option Explicit
Public Const STR_SPACE = "-" & vbTab
Public Function fnVarBubbleSort(ByRef varTempArray As Variant) As Variant
Dim varTemp As Variant
Dim lngCounter As Long
Dim blnNoExchanges As Boolean
Do
blnNoExchanges = True
For lngCounter = LBound(varTempArray) To UBound(varTempArray) - 1
If CDbl(varTempArray(lngCounter)) > CDbl(varTempArray(lngCounter + 1)) Then
blnNoExchanges = False
varTemp = varTempArray(lngCounter)
varTempArray(lngCounter) = varTempArray(lngCounter + 1)
varTempArray(lngCounter + 1) = varTemp
End If
Next lngCounter
Loop While Not (blnNoExchanges)
fnVarBubbleSort = varTempArray
On Error GoTo 0
Exit Function
End Function
Public Function fnListToArray(ByRef myList As Collection) As Variant
Dim lngCounter As Long
Dim myVar As Variant
ReDim myVar(myList.Count)
For lngCounter = 0 To myList.Count - 1
myVar(lngCounter) = myList(lngCounter + 1)
Next lngCounter
fnListToArray = myVar
End Function
Public Function fnArrayToList(ByRef myArray As Variant) As Collection
Dim lngCounter As Long
Dim myCol As New Collection
For lngCounter = LBound(myArray) To UBound(myArray)
myCol.Add myArray(lngCounter)
Next lngCounter
Set fnArrayToList = myCol
End Function
Public Sub TestMe()
Dim colCollection As New Collection
Dim varElement As Variant
colCollection.Add CDate("01.01.2011")
colCollection.Add CDate("01.01.2012")
colCollection.Add CDate("01.01.2011")
colCollection.Add CDate("01.01.2011")
colCollection.Add CDate("01.01.2011")
colCollection.Add CDate("01.01.2011")
colCollection.Add CDate("01.05.2015")
colCollection.Add CDate("01.01.2016")
colCollection.Add CDate("01.01.2011")
colCollection.Add CDate("01.01.2011")
colCollection.Add CDate("01.01.2011")
Set colCollection = fnArrayToList(fnVarBubbleSort(fnListToArray(colCollection)))
For Each varElement In colCollection
Debug.Print varElement
Next varElement
End Sub