Lodash _.sortedIndex() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Array

Definition

Uses a binary search to determine the lowest index at which value should be inserted into array to maintain its sort order. Part of Lodash Array since 0.1.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 _.sortedIndex on an array. Start with this line.

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

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

Library declarations
Lodash
_.sortedIndex(array, value)

Parameters

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

Return Value

Return ValueDescription
*Lowest index to insert while keeping sort order

Lodash Examples

Example 1 — Official: Insert in the Middle

Find where 40 belongs between 30 and 50.

Lodash
_.sortedIndex([30, 50], 40);
// → 1
Try It Yourself

How It Works

  1. Sorted source. [30, 50] is ascending.
  2. Compare. 40 is greater than 30, less than 50.
  3. Index. Lowest valid insert slot is 1.
  4. Result. Inserting there would yield [30, 40, 50].

Example 2 — Insert Before the First Element

A value smaller than every element maps to index 0.

Lodash
_.sortedIndex([30, 50], 10);
// → 0
Try It Yourself

How It Works

  1. Compare. 10 is less than 30.
  2. Left edge. Binary search ends at low = 0.
  3. Return. Insert at the front.
  4. After insert. Array would become [10, 30, 50].

Example 3 — Insert After the Last Element

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

Lodash
_.sortedIndex([30, 50], 60);
// → 2
Try It Yourself

How It Works

  1. Compare. 60 is greater than both elements.
  2. Right edge. Search settles at index 2 (the length).
  3. Append. That is the correct place for a new largest value.
  4. After insert. Array would become [30, 50, 60].

Example 4 — Duplicate Values (Bisect Left)

Equal values: return the lowest insert index among them.

Lodash
_.sortedIndex([10, 20, 20, 30], 20);
// → 1
Try It Yourself

How It Works

  1. Equals exist. Two 20s sit at indexes 1 and 2.
  2. Bisect left. Lodash chooses the leftmost slot → 1.
  3. Not rightmost. For after the run of equals, use _.sortedLastIndex → 3.
  4. Order. Either insert keeps ascending order.

Example 5 — Insert With splice

Use the index to mutate the array yourself.

Lodash
var nums = [30, 50];
var i = _.sortedIndex(nums, 40);
nums.splice(i, 0, 40);

console.log(i);    // 1
console.log(nums); // [30, 40, 50]
Try It Yourself

How It Works

  1. Locate. _.sortedIndex returns 1.
  2. Insert. splice(1, 0, 40) writes at that index.
  3. Mutate now. Only splice changes the array — not Lodash.
  4. Sorted stay. Result remains ascending.

Browser & Runtime Support

_.sortedIndex is a Lodash 4.x Array helper (since 0.1.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; binary-search insert helpers are library-level.

✓ Stable · Lodash 0.1.0+

_.sortedIndex()

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

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

Frequently Asked Questions

_.sortedIndex(array, value) uses a binary search to find the lowest index where value should be inserted into a sorted ascending array so the sort order stays valid. It does not insert the value — it only returns the index.
No. It only computes an insertion index. Use splice (or similar) yourself if you want to insert.
Yes — ascending order for the default comparator. An unsorted array produces meaningless indexes because the binary search assumes sorted data.
Lodash returns the leftmost insert point (bisect left) — the smallest index among equal values. For the rightmost insert point, use _.sortedLastIndex.
indexOf scans for an exact match in any array (linear). sortedIndex answers where to insert in a sorted array (logarithmic) and may return an index even when the value is not present.
Use import sortedIndex from "lodash/sortedIndex" or require("lodash/sortedIndex") so bundlers can tree-shake unused Lodash code. Full build: import _ from "lodash" then _.sortedIndex.

Next: _.sortedIndexBy()

Compute the insert index using a custom iteratee.

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