Rev 119 | Blame | Compare with Previous | Last modification | View Log | RSS feed
VERSION 1.0 CLASSBEGINMultiUse = -1 'TruePersistable = 0 'NotPersistableDataBindingBehavior = 0 'vbNoneDataSourceBehavior = 0 'vbNoneMTSTransactionMode = 0 'NotAnMTSObjectENDAttribute VB_Name = "CSorter"Attribute VB_GlobalNameSpace = FalseAttribute VB_Creatable = TrueAttribute VB_PredeclaredId = FalseAttribute VB_Exposed = TrueSub SwapRows(ary, row1, row2)'== This proc swaps two rows of an arrayDim x, tempvarFor x = 0 To UBound(ary, 1)tempvar = ary(x, row1)ary(x, row1) = ary(x, row2)ary(x, row2) = tempvarNextEnd SubSub QuickSort(vec, loBound, hiBound, SortField)'==--------------------------------------------------------=='== Sort a 2 dimensional array on SortField =='== =='== This procedure is adapted from the algorithm given in: =='== ~ Data Abstractions & Structures using C++ by ~ =='== ~ Mark Headington and David Riley, pg. 586 ~ =='== Quicksort is the fastest array sorting routine for =='== unordered arrays. Its big O is n log n =='== =='== Parameters: =='== vec - array to be sorted =='== SortField - The field to sort on (2nd dimension value) =='== loBound and hiBound are simply the upper and lower =='== bounds of the array's 1st dimension. It's probably =='== easiest to use the LBound and UBound functions to =='== set these. =='==--------------------------------------------------------==Dim pivot(), loSwap, hiSwap, temp, counterReDim pivot(UBound(vec, 1))'== Two items to sortIf hiBound - loBound = 1 ThenIf IsGreater(vec(SortField, loBound), vec(SortField, hiBound)) Then Call SwapRows(vec, hiBound, loBound)End If'== Three or more items to sortFor counter = 0 To UBound(vec, 1)pivot(counter) = vec(counter, Int((loBound + hiBound) / 2))vec(counter, Int((loBound + hiBound) / 2)) = vec(counter, loBound)vec(counter, loBound) = pivot(counter)NextloSwap = loBound + 1hiSwap = hiBoundDo'== Find the right loSwapWhile loSwap < hiSwap And (Not IsGreater(vec(SortField, loSwap), pivot(SortField)))loSwap = loSwap + 1Wend'== Find the right hiSwapWhile IsGreater(vec(SortField, hiSwap), pivot(SortField))hiSwap = hiSwap - 1Wend'== Swap values if loSwap is less then hiSwapIf loSwap < hiSwap Then Call SwapRows(vec, loSwap, hiSwap)Loop While loSwap < hiSwapFor counter = 0 To UBound(vec, 1)vec(counter, loBound) = vec(counter, hiSwap)vec(counter, hiSwap) = pivot(counter)Next'== Recursively call function .. the beauty of Quicksort'== 2 or more items in first sectionIf loBound < (hiSwap - 1) Then Call QuickSort(vec, loBound, hiSwap - 1, SortField)'== 2 or more items in second sectionIf hiSwap + 1 < hiBound Then Call QuickSort(vec, hiSwap + 1, hiBound, SortField)End SubFunction IsNaN(sString)Dim objRegExSet objRegEx = New RegExpobjRegEx.Global = TrueobjRegEx.IgnoreCase = TrueobjRegEx.Pattern = "\D"IsNaN = objRegEx.Test(sString)Set objRegEx = NothingEnd FunctionFunction IsGreater(ByVal sStringA, ByVal sStringB)Dim nMinLen, i, aCharsA, aCharsB, valA, valB'Dim tt'tt = TimerIsGreater = False'----------'IsGreater = Eval(sStringA > sStringB)'Exit Function'----------sStringA = Replace(sStringA, "-", ".")sStringB = Replace(sStringB, "-", ".")aCharsA = Split(sStringA, ".")aCharsB = Split(sStringB, ".")' Find which Ubound is smallestnMinLen = UBound(aCharsA)If nMinLen > UBound(aCharsB) Then nMinLen = UBound(aCharsB)'Response.write "======================================================<br>"'Response.write sStringA &" is greater then "& sStringB &"<br>"For i = 0 To nMinLenvalA = aCharsA(i)valB = aCharsB(i)If Not IsNaN(valA) And Not IsNaN(valB) Then' Do number compareIf CInt(valA) <> CInt(valB) ThenIf CInt(valA) > CInt(valB) ThenIsGreater = True'Response.write IsGreater &". Exiting number...<br>"Exit ForElse'Response.write IsGreater &". Exiting number...<br>"Exit ForEnd IfEnd IfElse' Do string compateIf valA <> valB ThenIf valA > valB ThenIsGreater = True'Response.write IsGreater &". Exiting string...<br>"Exit ForElse'Response.write IsGreater &". Exiting string...<br>"Exit ForEnd IfEnd IfEnd IfNext'Response.write "RESULT is greater:"& IsGreater &"<br>"'globaltt = globaltt + Timer - tt'Response.write "SwapRowsTIME " & Timer - tt & " Globaltt=" & globaltt & "<br>"End Function