by Anonymous (267 Submissions)
Category: Miscellaneous
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Sat 5th July 2003
Date Added: Mon 8th February 2021
Rating: (1 Votes)
Various sorts and functions to check if items exists in a sorted array or list
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
No comments have been posted about Various sorts and functions to check if items exists in a sorted array or list. Why not be the first to post a comment about Various sorts and functions to check if items exists in a sorted array or list.