@extra-array/bsearchr

2.1.75 • Public • Published

Binary searches rightmost value in sorted array.

Alternatives: default, closest, left, right.
This is part of package extra-array.

array.bsearchr(x, v, [fn]);
// x:  an array (sorted)
// v:  value to find
// fn: compare function (a, b)
// --> last index of value | ~(index of closest value)
const array = require('extra-array');

array.bsearchr([1, 3, 5, 7], 5);
// 2                  ^ found

array.bsearchr([1, 3, 5, 7], 4);
// -3 (~2)            ^ not found, closest

array.bsearchr([4, 4, 4, 4], 4);
// 3                     ^ rightmost

array.bsearchr(['b', 'GB', 'KB', 'MB'], 'kB', (a, b) => {
  return a.toLowerCase().localeCompare(b.toLowerCase());
});
// 2                        ^ case insensitive

references

Package Sidebar

Install

npm i @extra-array/bsearchr

Weekly Downloads

4

Version

2.1.75

License

MIT

Unpacked Size

3.09 kB

Total Files

4

Last publish

Collaborators

  • wolfram77