# Searchsorted with by and lookup

**URL:** <https://discourse.julialang.org/t/searchsorted-with-by-and-lookup/49952>\
**Category:** General Usage\
**Created:** [November 10, 2020, 10:53pm UTC](https://discourse.julialang.org/t/searchsorted-with-by-and-lookup/49952 "2020-11-10T22:53:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![endremborza](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/endremborza/32/19312_2.png) [@endremborza](https://discourse.julialang.org/u/endremborza)\
**Post date:** [November 10, 2020, 10:53pm UTC](https://discourse.julialang.org/t/searchsorted-with-by-and-lookup/49952/1 "2020-11-10T22:53:15Z")

</div>

```julia
a = [1,3,2,4,1,2,1];
is = sortperm(a);

```

after this, I’d assume `is[searchsorted(is, q, by=x -> a[x])]` should work to find indices in a that contain q, however:

```julia
a[is[searchsorted(is, 3, by=x -> a[x])]]
2-element Array{Int64,1}:
 2
 2

```

and similar queries somehow mess up. I might be missing something very simple but i feel quite stuck

works well with `searchsorted(a[is], q)`

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 11, 2020, 3:41pm UTC](https://discourse.julialang.org/t/searchsorted-with-by-and-lookup/49952/2 "2020-11-11T15:41:32Z")

</div>

I think that the implementation assumes that modifier keywords don’t break the ordering (otherwise, you can’t use binary search). See `?searchsorted`.

---

<div class="post-metadata">

**Author:** ![malacroi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/malacroi/32/19745_2.png) [@malacroi](https://discourse.julialang.org/u/malacroi)\
**Post date:** [November 11, 2020, 5:24pm UTC](https://discourse.julialang.org/t/searchsorted-with-by-and-lookup/49952/3 "2020-11-11T17:24:41Z")

</div>

> [@endremborza](#):
>
> `a[is[searchsorted(is, 3, by=x -> a[x])]]`

Your `searchsorted` has a `by` clause, so you’re searching for indices of `is` that give the same value as `3` after you apply the function `x->a[x]`, i.e. you’re searching for all the indices where `a[is]=a[3]=2`. This correctly returns the interval `4:5`. So

```julia
a[is[searchsorted(is, 3, by=x -> a[x])]]=a[is[4:5]]=a[[3,6]]=[2,2]

```

---

<div class="post-metadata">

**Author:** ![endremborza](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/endremborza/32/19312_2.png) [@endremborza](https://discourse.julialang.org/u/endremborza)\
**Post date:** [November 11, 2020, 6:28pm UTC](https://discourse.julialang.org/t/searchsorted-with-by-and-lookup/49952/4 "2020-11-11T18:28:17Z")

</div>

Oooh. I assumed that the query (x=3 here) does not go through the by function. Thank you.

Is there a built in function with this behavior?

---

<div class="post-metadata">

**Author:** ![endremborza](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/endremborza/32/19312_2.png) [@endremborza](https://discourse.julialang.org/u/endremborza)\
**Post date:** [November 12, 2020, 2:38am UTC](https://discourse.julialang.org/t/searchsorted-with-by-and-lookup/49952/5 "2020-11-12T02:38:43Z")

</div>

just to share, I found what I wanted, this way, using `view`:

```julia
a = [1,3,2,4,1,2,1];
is = sortperm(a);
va = view(a, is)
a[is[searchsorted(va, 3)]]

```
