Lodash _.sortedLastIndexBy() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Array

Definition

Like _.sortedLastIndex, but accepts an iteratee invoked for value and each element to compute their sort ranking (one argument: value). Returns the highest insert index (bisect right). Part of Lodash Array since 4.0.0 (stable — not deprecated). Warning: the array must already be sorted ascending by that ranking.

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

Syntax

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

Lodash
_.sortedLastIndexBy(array, value, [iteratee=_.identity])
CallWhat it does
_.sortedLastIndexBy(array, value, [iteratee=_.identity])Primary signature.
_.sortedLastIndexBy(array)Call with an array argument.

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

Library declarations
Lodash
_.sortedLastIndexBy(array, value, [iteratee=_.identity])

Parameters

ParameterDescription
arrayRequired. Sorted collection (by iteratee ranking)
valueRequired. Candidate to place (also passed through iteratee)
iterateeOptional. Invoked as (value); default _.identity

Return Value

Return ValueDescription
*Highest index to insert while keeping sort order

Lodash Examples

Example 1 — Official: Function Iteratee

Rank each object with a callback that returns o.x.

Lodash
var objects = [{ x: 4 }, { x: 5 }];

_.sortedLastIndexBy(objects, { x: 4 }, function (o) { return o.x; });
// → 1
Try It Yourself

How It Works

  1. Sorted by x. Rankings are 4 then 5.
  2. Candidate rank. Iteratee on { x: 4 } → 4.
  3. Bisect right. Insert after the equal 4 → index 1.
  4. No mutate. The array is unchanged until you insert.

Example 2 — Official: Property Shorthand

Pass "x" instead of writing a function.

Lodash
var objects = [{ x: 4 }, { x: 5 }];

_.sortedLastIndexBy(objects, { x: 4 }, "x");
// → 1
Try It Yourself

How It Works

  1. Shorthand. "x" means _.property("x").
  2. Same ranking. Compares o.x for every element and the value.
  3. Same result. Still 1 — after the equal run.
  4. Prefer. Use the string form for simple field sorts.

Example 3 — sortedLastIndexBy vs sortedIndexBy

Leftmost vs rightmost insert bounds around equal rankings.

Lodash
var objects = [{ x: 4 }, { x: 5 }];
var row = { x: 4 };

console.log(_.sortedIndexBy(objects, row, "x"));     // 0
console.log(_.sortedLastIndexBy(objects, row, "x")); // 1
Try It Yourself

How It Works

  1. Left. sortedIndexBy → insert before the first x: 4.
  2. Right. sortedLastIndexBy → insert after that equal ranking.
  3. Count. 1 - 0 = 1 object shares ranking 4.
  4. Pick. Use left for “new first equal”, right for “new last equal”.

Example 4 — Insert After the Last Object

A larger ranking maps to array.length.

Lodash
var objects = [{ x: 4 }, { x: 5 }];

_.sortedLastIndexBy(objects, { x: 9 }, "x");
// → 2
Try It Yourself

How It Works

  1. Compare. 9 is greater than 4 and 5.
  2. Right edge. Search settles at index 2.
  3. Append. That is the correct place for a new largest x.
  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 equal rankings.

Lodash
var objects = [{ x: 4 }, { x: 5 }];
var row = { x: 4 };
var i = _.sortedLastIndexBy(objects, row, "x");
objects.splice(i, 0, row);

console.log(i);       // 1
console.log(objects); // [{x:4},{x:4},{x:5}]
Try It Yourself

How It Works

  1. Locate. _.sortedLastIndexBy returns 1.
  2. Insert. splice(1, 0, row) writes after the equal ranking.
  3. Mutate now. Only splice changes the array — not Lodash.
  4. Sorted stay. Objects remain ordered by x.

Browser & Runtime Support

_.sortedLastIndexBy is a Lodash 4.x Array helper (since 4.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 4.0.0+

_.sortedLastIndexBy()

Available in [email protected] full builds and the lodash/sortedLastIndexBy 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
_.sortedLastIndexBy Lodash 4.0.0+

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

Frequently Asked Questions

_.sortedLastIndexBy(array, value, [iteratee=_.identity]) is like _.sortedLastIndex, but compares iteratee(value) against iteratee(each element). It returns the highest index where value should be inserted to keep the array sorted by that ranking.
sortedIndexBy is bisect left (lowest insert index). sortedLastIndexBy is bisect right (highest insert index). With equal rankings, they differ: before vs after the equal run.
No. It only returns an insertion index. Use splice yourself if you want to insert the object.
The array must already be sorted ascending by the iteratee result — for example, objects sorted by o.x when you pass "x" or (o) => o.x.
No. sortedLastIndexOf finds the last existing match (or -1). sortedLastIndexBy always returns an insert index in [0, length], even when the value is absent.
Use import sortedLastIndexBy from "lodash/sortedLastIndexBy" or require("lodash/sortedLastIndexBy") so bundlers can tree-shake unused Lodash code. Full build: import _ from "lodash" then _.sortedLastIndexBy.

Next: _.sortedLastIndexOf()

Binary-search for the last matching index of an existing value.

_.sortedLastIndexOf() 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