Suppose I have an array inventory::Vector{InventoryObject}
that is sorted by the name
attribute of each element (sort
with by=(obj->obj.name)
)
The InventoryObject
is defined as
@kwdef struct InventoryObject
name::String
domain::String = "jl"
role::String
priority::Int64 = 1
uri::String
dispname::String = "-"
end
Is there some way to use searchsorted
to find the range of indices for elements with a given x=name
? I’m not sure I really understand the by
and lt
arguments to searchsorted
, but they don’t seem to be completely helpful here. I’m missing a way to apply a transformation to each element of inventory
before comparing to the search value x
.
The only idea I’ve been able to come up with so far is to define a view like
struct InventoryNameView <: AbstractArray{Int, 1}
inventory::Vector{InventoryObject}
end
Base.size(view:: InventoryNameView) = size(view.inventory)
Base.getindex(view:: InventoryNameView, i::Int) = view.inventory[i].name
and call searchsorted
on the InventoryNameView
. Did I overlook something?
P.S.: I’d prefer to do this without adding new dependencies, with standard-library functions only.