_.sortedLastIndex(array, value)

39

_.sortedLastIndex(array, value)
_.sortedLastIndex类似sortedIndex方法,用二分查找来查找到一个最高位的值应该在哪个索引插入数组后还能够保持数组的排序
参数

array (Array): 需要查找元素索引的数组
value (*): 要查找的元素

返回值

(number): 返回应该插入元素的索引

例子

_.sortedLastIndex([4, 5, 5, 5, 6], 5);
// => 4

源代码:

代码调用了baseSortedIndex,前面看过了,省略

import baseSortedIndex from './.internal/baseSortedIndex.js'

/**
 * This method is like `sortedIndex` except that it returns the highest
 * index at which `value` should be inserted into `array` in order to
 * maintain its sort order.
 *
 * @since 3.0.0
 * @category Array
 * @param {Array} array The sorted array to inspect.
 * @param {*} value The value to evaluate.
 * @returns {number} Returns the index at which `value` should be inserted
 *  into `array`.
 * @example
 *
 * sortedLastIndex([4, 5, 5, 5, 6], 5)
 * // => 4
 */
//类似sortedIndex方法,用二分查找来查找到一个最高位的值应该在哪个索引插入数组后还能够保持数组的排序
function sortedLastIndex(array, value) {
  return baseSortedIndex(array, value, true)//调用baseSortedIndex,第三个参数是否返回最高位为true
}

export default sortedLastIndex

 

posted @ 2018-10-19 11:45  hahazexia  阅读(176)  评论(0)    收藏  举报