binarySearchInsertPoint method

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

Returns the insertion point for value (index where it would be inserted to preserve order).

compare must be consistent with the list's sort order.

Implementation

int binarySearchInsertPoint(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;
    if (cmp(this[mid], value) < 0) {
      low = mid + 1;
    } else {
      high = mid;
    }
  }
  return low;
}