find-insert-index
Version:
Find the index to insert an element in array keeping the sort order.
23 lines (20 loc) • 553 B
JavaScript
module.exports = (function () {
'use strict';
/**
* Find the index to insert an element in array keeping the sort order.
*
* @param {function} comparatorFn The comparator function which sorted the array.
* @param {array} arr The sorted array.
* @param {object} el The element to insert.
*/
function findInsertIndex(comparatorFn, arr, el) {
var i, len;
for (i = 0, len = arr.length; i < len; i++) {
if (comparatorFn(arr[i], el) > 0) {
break;
}
}
return i;
}
return findInsertIndex;
})();