@aegenet/belt-binary-search
v2.3.0
Published
Binary search
Downloads
51
Readme
@aegenet/belt-binary-search
Binary search
💾 Installation
yarn add @aegenet/belt-binary-search@^2.0.0
# or
npm i @aegenet/belt-binary-search@^2.0.0
📝 Usage
import { binarySearch } from '@aegenet/belt-binary-search';
binarySearch(
[1, 2, 3],
2,
(a, b) => a - b)
)
// Result
{ index: 1, value: 2 }
const sample = ['Arti', 'Morti', 'Lorti', 'Yolo', 'Yalo', 'Yago', 'Iago', 'Zorro', 'Nor', 'Aru', 'Guru'].sort();
binarySearch<string>(sample, 'Arti', (a, b) => a.localeCompare(b));
// Result
{ index: 0, value: 'Arti' }
const sample = [
{
id: 1,
code: 'Arti',
},
/** ... */
{
id: 11,
code: 'Guru',
},
].sort();
binarySearch<{ id: number; code?: string }>(
sample,
{ id: 1 },
(a, b) => a.id - b.id
);
// Result
{ index: 0, value: { id: 1, code: 'Arti' } }