static int intArrayBinarySearchWithComparator(int[] a, IntComparator comparator, int key) { ret intArrayBinarySearchWithComparator(a, 0, a.length, comparator, key); } static int intArrayBinarySearchWithComparator(int[] a, int fromIndex, int toIndex, IntComparator comparator, int key) { int low = fromIndex; int high = toIndex - 1; while (low <= high) { int mid = (low + high) >>> 1; int midVal = a[mid]; int cmp = comparator.compare(midVal, key); if (cmp < 0) low = mid + 1; else if (cmp > 0) high = mid - 1; else ret mid; // key found } ret -(low + 1); // key not found. }