Lodash _.sortedIndex() method

Beginner
⏱️ 7 min read
📚 Updated: May 2026
🎯 3 Code examples
🚀 3 Try-it labs
Lodash

What you’ll learn

  • How _.sortedIndex(array, value) picks the smallest index where value may land without breaking ascending order.
  • Why this is effectively binary-search / bisect-left on a sorted runway.
  • How duplicates and off-the-end inserts behave.
  • Try each example in the editor (?tryit=1, 2, 3) with Lodash from a CDN.

Prerequisites

Comfort with sorted arrays helps; contrast with linear lookups in _.indexOf() when data is not sorted.

  • You can reason about ascending order with default JavaScript comparisons (< for numbers, lexicographic order for strings).
  • You can run snippets in Node or open the Try-it labs in a browser.

Overview

_.sortedIndex answers “if I keep this array sorted ascending, where should this candidate sit?” without scanning every slot—ideal for incremental merges, leaderboard inserts, or backing sorted structures.

Binary search

Logarithmic probing replaces naive linear scans whenever the ordering invariant holds.

Bisect left

Duplicate runs resolve to the left edge—stable semantics for tie-heavy datasets.

Non-destructive

You decide whether to splice the value in afterward; Lodash only proposes the index.

Syntax

javascript
_.sortedIndex(array, value)
  • array: sorted collection inspected via binary search (array-like values are coerced).
  • value: candidate compared using ascending ordering expectations.
  • Returns: integer index in [0, array.length] where inserting value keeps ascending order.
1

Between existing entries

25 belongs between 20 and 30, so the insertion slot is index 2.

javascript
import sortedIndex from "lodash/sortedIndex";

const scores = [10, 20, 30, 40];
sortedIndex(scores, 25);
// → 2
Try it Yourself
2

Duplicate runs

Matching ties resolve to the leftmost valid insertion point—the classic bisect-left rule.

javascript
import sortedIndex from "lodash/sortedIndex";

const ranks = [1, 2, 2, 2, 3];
sortedIndex(ranks, 2);
// → 1
Try it Yourself
3

Prepend and append

Values smaller than everything map to 0; values larger than the tail map to length.

javascript
import sortedIndex from "lodash/sortedIndex";

sortedIndex([5, 15, 25], 2);   // → 0
sortedIndex([5, 15, 25], 99); // → 3
Try it Yourself

📋 _.sortedIndex vs _.sortedLastIndex vs _.indexOf

APIQuestion it answers
_.sortedIndex(array, value)Leftmost insertion index (bisect left) in a sorted ascending array.
_.sortedLastIndex(array, value)Rightmost insertion index (bisect right)—upper boundary for equal runs.
_.sortedIndexOf(array, value)Lowest matching index on sorted data, or -1 (binary hit-test).
_.indexOf(array, value)First exact match via linear scan; works on unsorted arrays.

Pitfalls to avoid

Sort

Unsorted input

Shuffle once and binary search lies quietly—sort or validate order before calling.

Cmp

Custom ordering

Default comparisons follow natural ascending rules; projections need _.sortedIndexBy(), and bespoke equality pairs need sortedLastIndex variants plus iteratees.

NaN

Odd compares

Values that fail comparisons (NaN, inconsistent objects) break ordering assumptions—sanitize upstream.

❓ FAQ

No. It only computes an insertion index; the collection you pass in stays unchanged.
Yes—ascending order with the default comparator. Feeding unsorted data yields meaningless indexes because the binary search assumes monotonic ordering.
Lodash returns the leftmost insertion index (bisect left): among equal runs, that is the smallest index where you could insert the value without breaking ascending order.
indexOf scans linearly and expects an exact match in arbitrary arrays. sortedIndex performs a binary search tailored to sorted arrays and answers where to insert, not whether the value is already present.
Use import sortedIndex from "lodash/sortedIndex" or require("lodash/sortedIndex") for tree-shaking friendly bundles.

Summary

  • Purpose: _.sortedIndex(array, value) returns the smallest insertion index that preserves ascending sort.
  • Requirement: the array must already be sorted ascending for the chosen comparator.
  • Next: Lodash _.sortedIndexBy(), _.slice() (previous), or the array methods hub.
Did you know?

Think of _.sortedIndex as a bisect-left helper: it assumes your data already honors ascending order—same precondition behind sortedLastIndex, sortedIndexOf, and the broader sortedIndex* family in the Lodash docs.

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.

6 people found this page helpful