4D v16.3Find in sorted array |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
4D v16.3
Find in sorted array
Find in sorted array
The Find in sorted array command returns true if at least one element in the sorted array matches the value, and optionally returns position(s) of matched element(s). Unlike Find in array, Find in sorted array only works with a sorted array and provides information about the position of occurrences, which allows you to insert elements if necessary. The array must be already sorted and must match the ordering specified by the > or < parameter (i.e. the "greater than" symbol for ascending order and the "lower than" symbol for descending order). The Find in sorted array command will take advantage of the sort and use a binary search algorithm, which is much more efficient for large arrays (for more information, please refer to the binary search algorithm page on Wikipedia). However, if the array is not properly sorted, the result may be incorrect. The command will ignore the sort indication and behave like a standard Find in array (sequential search, returning -1 for posFirst and posLast if the value is not found) in any of the following cases:
In case the command returns False, the value returned in posFirst can be passed to INSERT IN ARRAY to insert the value into the array while keeping the array sorted. This sequence is faster than inserting a new item at the end of the array and then calling SORT ARRAY to move it to the right place. The value returned in posLast can be combined with the value returned in posFirst to iterate on each element of the array matching the value (with a For...End for loop) or to find the number of occurrences (as would be found by Count in array, but faster). You want to insert a value, if necessary, while keeping the array sorted: C_LONGINT($pos) You want to find the number of occurrences of strings starting with "test" and create a string that concatenates all these elements: C_LONGINT($posFirst ;$posLast)
See also
|
PROPERTIES
Product: 4D HISTORY
Created: 4D v14 R4 ARTICLE USAGE
4D Language Reference ( 4D v16) |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||