Skip to content
This repository has been archived by the owner on Apr 18, 2020. It is now read-only.

Commit

Permalink
Update README to clarify comparator function
Browse files Browse the repository at this point in the history
  • Loading branch information
TrevorSayre committed Jan 31, 2018
1 parent b930e25 commit 30325e8
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,24 @@ Marino](https://github.com/mgmarino) for, ironically, pointing out bugs.
Example
-------

var bs = require("binary-search");
bs([1, 2, 3, 4], 3, function(a, b) { return a - b; }); // => 2
bs([1, 2, 4, 5], 3, function(a, b) { return a - b; }); // => -3
```js
var bs = require("binary-search");

bs([1, 2, 3, 4], 3, function(element, needle) { return element - needle; });
// => 2

bs([1, 2, 4, 5], 3, function(element, needle) { return element - needle; });
// => -3
```

Be advised that passing in a comparator function is *required*. Since you're
probably using one for your sort function anyway, this isn't a big deal.

The 3rd and 4th arguments to the comparator are the current index and array, respectively. You shouldn't normally need the index or array to compare values, but it's there if you do.
The comparator takes a 1st and 2nd argument of element and needle, respectively.

The comparator also takes a 3rd and 4th argument, the current index and array,
respectively. You shouldn't normally need the index or array to compare values,
but it's there if you do.

You may also, optionally, specify an input range as the final two parameters,
in case you want to limit the search to a particular range of inputs. However,
Expand Down

0 comments on commit 30325e8

Please sign in to comment.