# Bsearch issues

**URL:** <https://forum.crystal-lang.org/t/bsearch-issues/787>\
**Category:** Help & Support\
**Created:** [May 22, 2019, 2:54pm UTC](https://forum.crystal-lang.org/t/bsearch-issues/787 "2019-05-22T14:54:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jzakiya](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jzakiya/32/278_2.png) [@jzakiya](https://forum.crystal-lang.org/u/jzakiya)\
**Post date:** [May 22, 2019, 2:54pm UTC](https://forum.crystal-lang.org/t/bsearch-issues/787/1 "2019-05-22T14:54:01Z")

</div>

I’m trying to convert this Ruby code to Crystal.

```auto
haystack = [0,1,4,5,6,7,8,9,12,26,45,67,78,90,98,123,211,234,456,769,865,2345,3215,14345,24324]
needles = [0,42,45,24324,99999]
 
needles.select{|needle| haystack.bsearch{|hay| needle <=> hay} } # => [0, 45, 24324]

```

This code will run in Crystal, but not give the same (desired) result.

According to the Crystal docs for `bsearch`

[https://crystal-lang.org/api/0.28.0/Indexable.html#bsearch(%26block)-instance-method](https://crystal-lang.org/api/0.28.0/Indexable.html#bsearch%28%26block%29-instance-method)

you can’t use the `<=>` operator, and only shows examples using `>=`, `>`, `<`, and `<=`.

Does the current implementation of `bsearch` allow for searching for equality? and if so can you provide an example for this code example?

---

<div class="post-metadata">

**Author:** ![asterite](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/asterite/32/60_2.png) [@asterite](https://forum.crystal-lang.org/u/asterite)\
**Post date:** [May 22, 2019, 3:15pm UTC](https://forum.crystal-lang.org/t/bsearch-issues/787/2 "2019-05-22T15:15:26Z")

</div>

See [https://github.com/crystal-lang/crystal/issues/7727](https://github.com/crystal-lang/crystal/issues/7727)

Maybe we should reconsider it.

---

<div class="post-metadata">

**Author:** ![nsuchy](https://avatars.discourse-cdn.com/v4/letter/n/c0e974/32.png) [@nsuchy](https://forum.crystal-lang.org/u/nsuchy)\
**Post date:** [May 23, 2019, 3:18pm UTC](https://forum.crystal-lang.org/t/bsearch-issues/787/3 "2019-05-23T15:18:05Z")

</div>

> [@asterite](#):
>
> Maybe we should reconsider it.

If functionality doesn’t exist, I’m always open to adding it. If lets say sorting works slightly differently, I think the developer is just going to have to account for language differences and write their code to handle it accordingly. Thoughts?
