当前位置: 首页 > 文档资料 > Lazy.js 英文文档 >

sortedIndex

优质
小牛编辑
139浏览
2023-12-01

Performs a binary search of this sequence, returning the lowest index where the given value is either found, or where it belongs (if it is not already in the sequence).

This method assumes the sequence is in sorted order and will fail otherwise.

Signature

Sequence.sortedIndex = function(value) { /*...*/ }
Sequence.sortedIndex = function sortedIndex(value) {
  var indexed = this.getIndex(),
  lower   = 0,
  upper   = indexed.length(),
  i;

  while (lower < upper) {
i = (lower + upper) >>> 1;
if (compare(indexed.get(i), value) === -1) {
  lower = i + 1;
} else {
  upper = i;
}
  }
  return lower;
}
NameType(s)Description
value*

The element to search for in the sequence.

returnsnumber

An index within this sequence where the given value is located, or where it belongs in sorted order.

Examples

Lazy([1, 3, 6, 9]).sortedIndex(3)                    // => 1
Lazy([1, 3, 6, 9]).sortedIndex(7)                    // => 3
Lazy([5, 10, 15, 20]).filter(isEven).sortedIndex(10) // => 0
Lazy([5, 10, 15, 20]).filter(isEven).sortedIndex(12) // => 1