binarySearchIndex method

int binarySearchIndex(
  1. T value, [
  2. int compare(
    1. T a,
    2. T b
    )?
])

Returns the index of value in this sorted list, or -1 if not found.

compare must be consistent with the list's sort order. Defaults to Comparable.compare. List must be sorted in ascending order.

Implementation

int binarySearchIndex(T value, [int Function(T a, T b)? compare]) {
  final int Function(T a, T b) cmp =
      compare ??
      (T a, T b) => a is Comparable<dynamic>
          ? a.compareTo(b)
          : (throw ArgumentError(_kErrTMustImplementComparable));
  int low = 0;
  int high = length;
  while (low < high) {
    final int mid = (low + high) >> 1;
    final int c = cmp(this[mid], value);
    if (c < 0) {
      low = mid + 1;
    } else if (c > 0) {
      high = mid;
    } else {
      return mid;
    }
  }
  return -1;
}