|
|
Joined: Dec 1999
Posts: 30,867
Master Elite Member
|
Public Function SortIndexOfSArray(pArray() As String, pString As String, pCompare As VbCompareMethod) As Long
'Function
' Used to determine what index a string will be inserted into an array
' in order for it to be properly sorted.
'Parameters
' pArray (String()) = This is the array we are using to locate an index in.
' pString (String) = This is the string we are finding an index for.
' pCompare (vbCompareMethod) = Determines what type of search will be done.
' Valid Values: vbTextCompare, vbBinaryCompare
'Return (Long)
' Returns the index at which a pString would be inserted into an pArray in order for it
' to be properly sorted.
' If the string is not to be inserted before any of the elements in the array
' (ie. it should be placed at the end), function returns the highest index in the array plus 1.
' Returns 0 if the String is to be inserted at the very top of the array.
' Returns 0 if the specified array (pArray) is empty.
'Entry Conditions
' pCompare must be a valid value (specified above) or the result of this function is undefined
' and it may crash.
'Notes
' This function can handle non-zero-based arrays.
'Version History
' 1.0 7/21/03 Tested
'---
'Initiate variables
Dim C As Long
'Trap: The specified array is empty, return 0 and exit now
If IsEmptyArray(pArray) Then
SortIndexOfSArray = 0
Exit Function
End If
'Loop through each element in pArray to determine where the string should be placed
For C = LBound(pArray) To UBound(pArray)
'The specified string should be inserted before this element, return it's index and exit now
If StrComp(pString, pArray(C), pCompare) = -1 Then
SortIndexOfSArray = C
Exit Function
End If
Next C
'The specified string should not be inserted before any elements in the array
'(it should be appended to the end), return the index of the last element plus 1
SortIndexOfSArray = UBound(pArray) + 1
End Function
|
|
|
|
Entire Thread
|
Used to determine what index a string will be inserted into an array
|
Russ
|
08/11/08 10:31 PM
|
|
|
|