Lodash _.sortedLastIndex() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Array

Definition

Like _.sortedIndex, except it returns the highest index at which value should be inserted to maintain sort order (bisect right). Part of Lodash Array since 3.0.0 (stable in 4.x — not deprecated). Warning: the array must already be sorted ascending.

Pass the required arguments shown below. Lodash Array helpers usually return a new value — check whether the source array is mutated.

Syntax

Call _.sortedLastIndex on an array. Start with this line.

Lodash
_.sortedLastIndex(array, value)
CallWhat it does
_.sortedLastIndex(array, value)Primary signature.
_.sortedLastIndex(array)Call with an array argument.

Prefer import sortedLastIndex from "lodash/sortedLastIndex" when you want a smaller bundle.

Library declarations
Lodash
_.sortedLastIndex(array, value)

Parameters

ParameterDescription
arrayRequired. Sorted ascending collection to inspect
valueRequired. Value whose highest insert index you want

Return Value

Return ValueDescription
*Highest index to insert while keeping sort order

Lodash Examples

Example 1 — Official: After Duplicate Run

Insert 5 after all existing 5s.

Lodash
_.sortedLastIndex([4, 5, 5, 5, 6], 5);
// → 4
Try It Yourself

How It Works

  1. Sorted source. Three 5s sit at indexes 1…3.
  2. Bisect right. Highest safe insert is just after that run.
  3. Index. Slot 4 is before 6.
  4. Result. Inserting there appends another 5 after equals.

Example 2 — sortedLastIndex vs sortedIndex

Leftmost vs rightmost insert bounds around duplicates.

Lodash
var arr = [4, 5, 5, 5, 6];

console.log(_.sortedIndex(arr, 5));     // 1
console.log(_.sortedLastIndex(arr, 5)); // 4
Try It Yourself

How It Works

  1. Left. sortedIndex → insert before the first 5.
  2. Right. sortedLastIndex → insert after the last 5.
  3. Count. 4 - 1 = 3 equal values in between.
  4. Pick. Use left for “new first equal”, right for “new last equal”.

Example 3 — Insert Before the First Element

A value smaller than every element maps to index 0.

Lodash
_.sortedLastIndex([4, 5, 5, 5, 6], 3);
// → 0
Try It Yourself

How It Works

  1. Compare. 3 is less than 4.
  2. No equals. Left and right bounds collapse to 0.
  3. Return. Same answer as _.sortedIndex(arr, 3).
  4. After insert. Array would become [3, 4, 5, 5, 5, 6].

Example 4 — Insert After the Last Element

A value larger than every element maps to array.length.

Lodash
_.sortedLastIndex([4, 5, 5, 5, 6], 7);
// → 5
Try It Yourself

How It Works

  1. Compare. 7 is greater than every element.
  2. Right edge. Search settles at index 5 (the length).
  3. Append. That is the correct place for a new largest value.
  4. Same as left. With no equals, left and right bounds match.

Example 5 — Insert With splice

Use the upper bound to append after existing equals.

Lodash
var nums = [4, 5, 5, 5, 6];
var i = _.sortedLastIndex(nums, 5);
nums.splice(i, 0, 5);

console.log(i);    // 4
console.log(nums); // [4, 5, 5, 5, 5, 6]
Try It Yourself

How It Works

  1. Locate. _.sortedLastIndex returns 4.
  2. Insert. splice(4, 0, 5) writes after the equal run.
  3. Mutate now. Only splice changes the array — not Lodash.
  4. Sorted stay. Result remains ascending with four 5s.

Browser & Runtime Support

_.sortedLastIndex is a Lodash 4.x Array helper (since 3.0.0). It runs anywhere Lodash runs — modern browsers (via CDN or bundle) and Node.js / Bun / Deno with npm install lodash. There is no single native Array API with the same name.

✓ Stable · Lodash 3.0.0+

_.sortedLastIndex()

Available in [email protected] full builds and the lodash/sortedLastIndex modular import. Works in evergreen browsers and current Node LTS when Lodash is installed.

100% With Lodash 4.x
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
_.sortedLastIndex Lodash 3.0.0+

Bottom line: Safe for production once Lodash is on the page or in your bundle. Prefer import sortedLastIndex from "lodash/sortedLastIndex" for smaller builds. Not a native browser API — load Lodash first.

Frequently Asked Questions

_.sortedLastIndex(array, value) uses a binary search to find the highest index where value should be inserted into a sorted ascending array so sort order stays valid. It does not insert the value — it only returns the index.
sortedIndex is bisect left (lowest insert index). sortedLastIndex is bisect right (highest insert index). With duplicates, they differ: insert before the equal run vs after it.
No. It only computes an insertion index. Use splice yourself if you want to insert.
Yes — ascending order. An unsorted array produces meaningless indexes because the binary search assumes sorted data.
Any integer from 0 through array.length inclusive — same span as _.sortedIndex.
Use import sortedLastIndex from "lodash/sortedLastIndex" or require("lodash/sortedLastIndex") so bundlers can tree-shake unused Lodash code. Full build: import _ from "lodash" then _.sortedLastIndex.

Next: _.sortedLastIndexBy()

Compute the highest insert index using a custom iteratee.

_.sortedLastIndexBy() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful