UNPKG

array-push-at-sort-position

Version:

Push items to an array at their correct sort-position

45 lines (42 loc) 1.04 kB
/** * copied and adapted from npm 'binary-search-insert' * @link https://www.npmjs.com/package/binary-search-insert */ export function pushAtSortPosition(array, item, compareFunction, low) { var length = array.length; var high = length - 1; var mid = 0; /** * Optimization shortcut. */ if (length === 0) { array.push(item); return 0; } /** * So we do not have to get the ret[mid] doc again * at the last we store it here. */ var lastMidDoc; while (low <= high) { // https://github.com/darkskyapp/binary-search // http://googleresearch.blogspot.com/2006/06/extra-extra-read-all-about-it-nearly.html mid = low + (high - low >> 1); lastMidDoc = array[mid]; if (compareFunction(lastMidDoc, item) <= 0.0) { // searching too low low = mid + 1; } else { // searching too high high = mid - 1; } } if (compareFunction(lastMidDoc, item) <= 0.0) { mid++; } /** * Insert at correct position */ array.splice(mid, 0, item); return mid; }