searchsortedfirst

searchsortedfirst(a, x, [by=,] [lt=,] [rev=false])

Returns the index of the first value in a greater than or equal to x, according to the specified order. Returns length(a)+1 if x is greater than all values in a.

Examples

# Search for the index of the first value greater than or equal to x

julia> arr = [1, 2, 3, 4, 5];
julia> searchsortedfirst(arr, 3)
3

# Search for the index using a custom ordering function

julia> arr = ["apple", "banana", "cherry", "date"];
julia> searchsortedfirst(arr, "c", lt=bylength)
2

# Reverse search in a sorted array

julia> arr = [5, 4, 3, 2, 1];
julia> searchsortedfirst(arr, 3, rev=true)
3

# Handling values greater than all elements

julia> arr = [10, 20, 30, 40, 50];
julia> searchsortedfirst(arr, 60)
6

Common mistake example:

julia> arr = [1, 2, 3, 4, 5];
julia> searchsortedfirst(arr, 6)
6

In this example, 6 is greater than all values in the array arr, so length(arr)+1 is returned. It's important to handle the case where x is greater than all elements in a to avoid unexpected behavior.

See Also

find, findfirst, findin, findlast, findmin, findn, findnext, findnz, findprev, rsearch, rsearchindex, searchsorted, searchsortedfirst, searchsortedlast, sort, sort!, sortcols, sortperm, sortperm!,

User Contributed Notes

Add a Note

The format of note supported is markdown, use triple backtick to start and end a code block.

*Required Field
Details

Checking you are not a robot: