Subversion Repositories DevTools

Rev

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