Lodash _.sortedLastIndexOf() method
What you’ll learn
- How
_.sortedLastIndexOf(array, value)resolves to the last index whose element equalsvalue. - Why duplicate bands pair naturally with _.sortedIndexOf().
- When logarithmic
sortedLastIndexOfbeats _.lastIndexOf() on long sorted buffers. - Try each example in the editor (
?tryit=1,2,3) with Lodash from a CDN.
Prerequisites
Read _.sortedIndexOf() for first-hit semantics; skim _.lastIndexOf() if reverse linear scans are familiar territory.
- Your collection stays sorted ascending under default comparisons.
- You can run snippets in Node or open the Try-it labs in a browser.
Overview
_.sortedLastIndexOf answers “where does this value finally appear before larger neighbors arrive?” while retaining logarithmic cost thanks to sorted guarantees.
Binary tail seek
Skip scanning backwards manually—Lodash finds the tail of an equal span directly.
SameValueZero
Equality semantics align with indexOf-style helpers, including sensible NaN handling.
Honest misses
-1 keeps parity with unsorted lastIndexOf expectations.
Syntax
_.sortedLastIndexOf(array, value) - array: ascending sorted collection inspected via binary search.
- value: needle tested with SameValueZero equality.
- Returns: greatest matching index, or
-1when absent.
Last duplicate slot
Within a plateau of equal numbers the answer hugs the right edge—here index 3 before 30 resumes.
import sortedLastIndexOf from "lodash/sortedLastIndexOf";
const tiers = [10, 20, 20, 20, 30];
sortedLastIndexOf(tiers, 20);
// → 3 Value absent
Missing needles never masquerade as insertion hints—you still read -1.
import sortedLastIndexOf from "lodash/sortedLastIndexOf";
sortedLastIndexOf([1, 3, 5, 7], 4);
// → -1 Bracket with sortedIndexOf
Subtract first from last and add one to count identical entries without walking the span.
import sortedIndexOf from "lodash/sortedIndexOf";
import sortedLastIndexOf from "lodash/sortedLastIndexOf";
const xs = [5, 10, 10, 10, 15];
const v = 10;
const count = sortedLastIndexOf(xs, v) - sortedIndexOf(xs, v) + 1;
// → 3 📋 _.sortedLastIndexOf vs _.sortedIndexOf vs _.lastIndexOf
| API | Duplicate band | Requires sorted input? |
|---|---|---|
_.sortedIndexOf(array, value) | Returns first equal index (or -1). | Yes |
_.sortedLastIndexOf(array, value) | Returns last equal index (or -1). | Yes |
_.lastIndexOf(array, value) | Last match via reverse linear scan. | No |
Pitfalls to avoid
Stale ordering
Partial inserts or descending slices invalidate assumptions—resort or guard before searching.
Insertion helpers
Do not confuse with _.sortedLastIndex(), which always emits an insertion fence even when the needle is missing.
Off-by-one counts
Inclusive duplicate counts need + 1 after subtracting first from last index.
❓ FAQ
Summary
- Purpose:
_.sortedLastIndexOf(array, value)returns the final matching index on sorted data, else-1. - Pairing: combine with
sortedIndexOffor duplicate analytics without linear walks. - Next: Lodash _.sortedUniq(), _.sortedLastIndexBy() (previous), or the array methods hub.
_.sortedIndexOf() lands on the first equal slot; sortedLastIndexOf lands on the final one—subtract and add one (or slice between fences) to measure how wide a plateau is.
6 people found this page helpful
