Lodash _.sortedIndexBy() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Array

Definition

Like _.sortedIndex, but accepts an iteratee invoked for value and each element to compute their sort ranking (one argument: value). 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 _.sortedIndexBy on an array. Start with this line.

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

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

Library declarations
Lodash
_.sortedIndexBy(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
*Lowest 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 }];

_.sortedIndexBy(objects, { x: 4 }, function (o) { return o.x; });
// → 0
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 left. Equal to the first ranking → insert at 0.
  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 }];

_.sortedIndexBy(objects, { x: 4 }, "x");
// → 0
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 0 — leftmost among equals.
  4. Prefer. Use the string form for simple field sorts.

Example 3 — Insert Between Objects

A ranking between two neighbors lands in the middle.

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

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

How It Works

  1. Rankings. Source ranks: 2, 4, 6.
  2. Candidate. { x: 5 } ranks as 5.
  3. Slot. After 4, before 6 → index 2.
  4. Order. Inserting there keeps ascending x.

Example 4 — Insert After the Last Object

A larger ranking maps to array.length.

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

_.sortedIndexBy(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. After insert. Array would become [{x:4},{x:5},{x:9}].

Example 5 — Insert With splice

Use the index to mutate the array yourself.

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

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

How It Works

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

Browser & Runtime Support

_.sortedIndexBy 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+

_.sortedIndexBy()

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

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

Frequently Asked Questions

_.sortedIndexBy(array, value, [iteratee=_.identity]) is like _.sortedIndex, but compares iteratee(value) against iteratee(each element). It returns the lowest index where value should be inserted to keep the array sorted by that ranking.
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.
sortedIndex compares elements directly (numbers, strings). sortedIndexBy projects each element through an iteratee first — ideal for arrays of objects.
Yes. Lodash invokes the same iteratee for value and for every array element so comparisons stay symmetric.
Use import sortedIndexBy from "lodash/sortedIndexBy" or require("lodash/sortedIndexBy") so bundlers can tree-shake unused Lodash code. Full build: import _ from "lodash" then _.sortedIndexBy.

Next: _.sortedIndexOf()

Binary-search for the index of an existing value in a sorted array.

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