bisectLeft<T extends Comparable<Object>> function

int bisectLeft<T extends Comparable<Object>>(
  1. List<T> list,
  2. T value, {
  3. int lo = 0,
  4. int? hi,
})

Returns the insertion index for value in list to maintain sorted order. Uses the leftmost position for equal values.

Implementation

int bisectLeft<T extends Comparable<Object>>(
  List<T> list,
  T value, {
  int lo = 0,
  int? hi,
}) {
  var hiValue = hi ?? list.length;

  while (lo < hiValue) {
    final mid = (lo + hiValue) ~/ 2;
    if (list[mid].compareTo(value) < 0) {
      lo = mid + 1;
    } else {
      hiValue = mid;
    }
  }

  return lo;
}