Subversion Repositories DevTools

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
119 ghuddy 1
VERSION 1.0 CLASS
2
BEGIN
3
  MultiUse = -1  'True
4
  Persistable = 0  'NotPersistable
5
  DataBindingBehavior = 0  'vbNone
6
  DataSourceBehavior  = 0  'vbNone
7
  MTSTransactionMode  = 0  'NotAnMTSObject
8
END
9
Attribute VB_Name = "CSorter"
10
Attribute VB_GlobalNameSpace = False
11
Attribute VB_Creatable = True
12
Attribute VB_PredeclaredId = False
13
Attribute VB_Exposed = True
14
Sub SwapRows(ary, row1, row2)
15
    '== This proc swaps two rows of an array
16
    Dim x, tempvar
17
 
18
    For x = 0 To UBound(ary, 1)
19
        tempvar = ary(x, row1)
20
        ary(x, row1) = ary(x, row2)
21
        ary(x, row2) = tempvar
22
    Next
23
 
24
End Sub
25
 
26
Sub QuickSort(vec, loBound, hiBound, SortField)
27
 
28
    '==--------------------------------------------------------==
29
    '== Sort a 2 dimensional array on SortField                ==
30
    '==                                                        ==
31
    '== This procedure is adapted from the algorithm given in: ==
32
    '==    ~ Data Abstractions & Structures using C++ by ~     ==
33
    '==    ~ Mark Headington and David Riley, pg. 586    ~     ==
34
    '== Quicksort is the fastest array sorting routine for     ==
35
    '== unordered arrays.  Its big O is  n log n               ==
36
    '==                                                        ==
37
    '== Parameters:                                            ==
38
    '== vec       - array to be sorted                         ==
39
    '== SortField - The field to sort on (2nd dimension value) ==
40
    '== loBound and hiBound are simply the upper and lower     ==
41
    '==   bounds of the array's 1st dimension.  It's probably  ==
42
    '==   easiest to use the LBound and UBound functions to    ==
43
    '==   set these.                                           ==
44
    '==--------------------------------------------------------==
45
 
46
 
47
 
48
    Dim pivot(), loSwap, hiSwap, temp, counter
49
    ReDim pivot(UBound(vec, 1))
50
 
51
    '== Two items to sort
52
    If hiBound - loBound = 1 Then
53
        If IsGreater(vec(SortField, loBound), vec(SortField, hiBound)) Then Call SwapRows(vec, hiBound, loBound)
54
    End If
55
 
56
    '== Three or more items to sort
57
    For counter = 0 To UBound(vec, 1)
58
        pivot(counter) = vec(counter, Int((loBound + hiBound) / 2))
59
        vec(counter, Int((loBound + hiBound) / 2)) = vec(counter, loBound)
60
        vec(counter, loBound) = pivot(counter)
61
    Next
62
 
63
    loSwap = loBound + 1
64
    hiSwap = hiBound
65
 
66
    Do
67
        '== Find the right loSwap
68
        While loSwap < hiSwap And (Not IsGreater(vec(SortField, loSwap), pivot(SortField)))
69
            loSwap = loSwap + 1
70
        Wend
71
 
72
        '== Find the right hiSwap
73
        While IsGreater(vec(SortField, hiSwap), pivot(SortField))
74
        hiSwap = hiSwap - 1
75
        Wend
76
 
77
 
78
        '== Swap values if loSwap is less then hiSwap
79
        If loSwap < hiSwap Then Call SwapRows(vec, loSwap, hiSwap)
80
 
81
    Loop While loSwap < hiSwap
82
 
83
    For counter = 0 To UBound(vec, 1)
84
        vec(counter, loBound) = vec(counter, hiSwap)
85
        vec(counter, hiSwap) = pivot(counter)
86
    Next
87
 
88
    '== Recursively call function .. the beauty of Quicksort
89
    '== 2 or more items in first section
90
    If loBound < (hiSwap - 1) Then Call QuickSort(vec, loBound, hiSwap - 1, SortField)
91
 
92
    '== 2 or more items in second section
93
    If hiSwap + 1 < hiBound Then Call QuickSort(vec, hiSwap + 1, hiBound, SortField)
94
 
95
End Sub
96
 
97
Function IsNaN(sString)
98
    Dim objRegEx
99
 
100
    Set objRegEx = New RegExp
101
 
102
    objRegEx.Global = True
103
    objRegEx.IgnoreCase = True
104
    objRegEx.Pattern = "\D"
105
 
106
    IsNaN = objRegEx.Test(sString)
107
 
108
    Set objRegEx = Nothing
109
 
110
 
111
End Function
112
 
113
Function IsGreater(ByVal sStringA, ByVal sStringB)
114
    Dim nMinLen, i, aCharsA, aCharsB, valA, valB
115
 
116
    'Dim tt
117
 
118
    'tt = Timer
119
 
120
 
121
    IsGreater = False
122
 
123
 
124
    '----------
125
    'IsGreater = Eval(sStringA > sStringB)
126
    'Exit Function
127
    '----------
128
 
129
    sStringA = Replace(sStringA, "-", ".")
130
    sStringB = Replace(sStringB, "-", ".")
131
 
132
 
133
    aCharsA = Split(sStringA, ".")
134
    aCharsB = Split(sStringB, ".")
135
 
136
 
137
    ' Find which Ubound is smallest
138
    nMinLen = UBound(aCharsA)
139
    If nMinLen > UBound(aCharsB) Then nMinLen = UBound(aCharsB)
140
 
141
 
142
 
143
 
144
    'Response.write "======================================================<br>"
145
 
146
    'Response.write sStringA &" is greater then "& sStringB  &"<br>"
147
 
148
    For i = 0 To nMinLen
149
 
150
 
151
        valA = aCharsA(i)
152
        valB = aCharsB(i)
153
 
154
 
155
        If Not IsNaN(valA) And Not IsNaN(valB) Then
156
            ' Do number compare
157
            If CInt(valA) <> CInt(valB) Then
158
 
159
                If CInt(valA) > CInt(valB) Then
160
                    IsGreater = True
161
                    'Response.write IsGreater &". Exiting number...<br>"
162
 
163
                    Exit For
164
                Else
165
                    'Response.write IsGreater &". Exiting number...<br>"
166
 
167
                    Exit For
168
                End If
169
 
170
            End If
171
 
172
        Else
173
            ' Do string compate
174
            If valA <> valB Then
175
 
176
                If valA > valB Then
177
                    IsGreater = True
178
                    'Response.write IsGreater &". Exiting string...<br>"
179
 
180
                    Exit For
181
                Else
182
                    'Response.write IsGreater &". Exiting string...<br>"
183
 
184
                    Exit For
185
                End If
186
 
187
            End If
188
 
189
        End If
190
 
191
 
192
    Next
193
 
194
    'Response.write "RESULT is greater:"& IsGreater  &"<br>"
195
    'globaltt = globaltt + Timer - tt
196
    'Response.write "SwapRowsTIME " & Timer - tt & " Globaltt=" & globaltt & "<br>"
197
End Function
198