|
Visual Basic (VB and VBA) |
|
Copyright 1999-2001 Christopher Greaves. All rights reserved. Home Page and email to [email protected] |
| If in doubt, record a macro and inspect the entrails! |
Please read the DISCLAIMER.
Here is an INDEX to all the procedures.
You will probably need one copy of my GLOBAL DECLARATIONS.
Public Function strSort(strList() As String, boolCaseSensitive As Boolean, ByVal lLbound As Long, ByVal lUbound As Long)
' Procedure : strSort
' Description: Sorts a string array.
' Copyright: Chris Greaves Inc.
' Inputs: A possibly empty array of strings.
' Returns: The input array, sorted in sequence; optionally case-sensitive.
' Assumes: Nothing
' Side Effects: None.
' Tested: By the calls shown below.
Dim strTemp As String
Dim strBuffer As String
Dim lngCurLow As Long
Dim lngCurHigh As Long
Dim lngCurMidpoint As Long
If lUbound <= lLbound Then ' Oops! get it right!!
lngCurLow = lLbound
lLbound = lUbound
lUbound = lngCurLow
End If
lngCurLow = lLbound ' Start current low and high at actual low/high
lngCurHigh = lUbound
lngCurMidpoint = (lLbound + lUbound) \ 2 ' Find the approx midpoint of the array
strTemp = strUCase(strList(lngCurMidpoint), boolCaseSensitive) ' Pick as a starting point.
Do While (lngCurLow <= lngCurHigh)
' Creep lngCurRow up towards a position equal to the VALUE of the mid point.
Do While strUCase(strList(lngCurLow), boolCaseSensitive) < strTemp
lngCurLow = lngCurLow + 1
If lngCurLow = lUbound Then Exit Do
Loop
' Creep lngCurRow down towards a position equal to the VALUE of the mid point.
Do While strTemp < strUCase(strList(lngCurHigh), boolCaseSensitive)
lngCurHigh = lngCurHigh - 1
If lngCurHigh = lLbound Then Exit Do
Loop
' If the values are either side the value of the midpoint, then swap them
If (lngCurLow <= lngCurHigh) Then ' if low is <= high then swap them ..
strBuffer = strList(lngCurLow)
strList(lngCurLow) = strList(lngCurHigh)
strList(lngCurHigh) = strBuffer
' .. and sort the restricted array.
lngCurLow = lngCurLow + 1 ' CurLow++
lngCurHigh = lngCurHigh - 1 ' CurLow--
End If
Loop
If lLbound < lngCurHigh Then ' Recurse if necessary with the upper-portion of the array.
strSort strList(), boolCaseSensitive, lLbound, lngCurHigh
End If
If lngCurLow < lUbound Then ' Recurse if necessary with the lower portion of the array.
strSort strList(), boolCaseSensitive, lngCurLow, lUbound
End If
'Sub TESTstrSort()
'Dim strList(9) As String
'Dim i As Integer
'strList(0) = "4"
'strList(1) = "3"
'strList(2) = "5"
'strList(3) = "2"
'strList(4) = "6"
'strList(5) = "9"
'strList(6) = "0"
'strList(7) = "1"
'strList(8) = "7"
'strList(9) = "8"
'Call strSort(strList, False, LBound(strList), UBound(strList))
'For i = 0 To 9
' Debug.Print strList(i)
'Next i
'End Sub
End Function
| We all knew nothing when we started … |
|
Home Page and Contact Information Send email to [email protected]. This page was last updated Thursday, November 15, 2001 |