indexOf<T extends Comparable<Object>> function

int indexOf<T extends Comparable<Object>>(
  1. List<T> list,
  2. T value
)

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

Implementation

int indexOf<T extends Comparable<Object>>(List<T> list, T value) {
  int lo = 0;
  int hi = list.length;

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

  return -1;
}