- Home
·
- Miscellaneous
·
- Various sorts and functions to check if items exists in a sorted array or list
Various sorts and functions to check if items exists in a sorted array or list
Various sorts and functions to check if items exists in a sorted array or list
Rate Various sorts and functions to check if items exists in a sorted array or list
(1(1 Vote))
Public Function listItemExists(find As String, x As Control) As Integer
Dim i%, lo%, hi%, c%
lo = 0
hi = x.ListCount - 1
If hi > -1 Then
Do While (lo <= hi)
i = (lo + hi) / 2
c = StrComp(CStr(x.list(i)), find)
'if found return the index
If c = 0 Then
listItemExists = i
Exit Function
'otherwise continue searching in the remaining lower or upper half
ElseIf c = -1 Then
lo = i + 1
Else
hi = i - 1
End If
Loop
End If
listItemExists = -1
End Function
'note: this one only works with sorted string arrays
Public Function itemExists(s As String, Arr() As String) As Boolean
Dim i%, lo%, hi%, c%
lo = LBound(Arr)
hi = UBound(Arr)
Do While (lo <= hi)
i = (lo + hi) / 2
c = StrComp(CStr(Arr(i)), s, vbTextCompare)
If c = 0 Then
itemExists = True
Exit Function
ElseIf c = -1 Then
lo = i + 1
Else
hi = i - 1
End If
Loop
itemExists = False
End Function
Various sorts and functions to check if items exists in a sorted array or list Comments
No comments yet — be the first to post one!
Post a Comment