Lodash _.sortedIndex() method
What you’ll learn
- How
_.sortedIndex(array, value)picks the smallest index wherevaluemay 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
_.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 insertingvaluekeeps ascending order.
Between existing entries
25 belongs between 20 and 30, so the insertion slot is index 2.
import sortedIndex from "lodash/sortedIndex";
const scores = [10, 20, 30, 40];
sortedIndex(scores, 25);
// → 2 Duplicate runs
Matching ties resolve to the leftmost valid insertion point—the classic bisect-left rule.
import sortedIndex from "lodash/sortedIndex";
const ranks = [1, 2, 2, 2, 3];
sortedIndex(ranks, 2);
// → 1 Prepend and append
Values smaller than everything map to 0; values larger than the tail map to length.
import sortedIndex from "lodash/sortedIndex";
sortedIndex([5, 15, 25], 2); // → 0
sortedIndex([5, 15, 25], 99); // → 3 📋 _.sortedIndex vs _.sortedLastIndex vs _.indexOf
| API | Question 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
Unsorted input
Shuffle once and binary search lies quietly—sort or validate order before calling.
Custom ordering
Default comparisons follow natural ascending rules; projections need _.sortedIndexBy(), and bespoke equality pairs need sortedLastIndex variants plus iteratees.
Odd compares
Values that fail comparisons (NaN, inconsistent objects) break ordering assumptions—sanitize upstream.
❓ FAQ
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.
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.
6 people found this page helpful
