Lodash _.sortedLastIndexOf() method

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

What you’ll learn

  • How _.sortedLastIndexOf(array, value) resolves to the last index whose element equals value.
  • Why duplicate bands pair naturally with _.sortedIndexOf().
  • When logarithmic sortedLastIndexOf beats _.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

javascript
_.sortedLastIndexOf(array, value)
  • array: ascending sorted collection inspected via binary search.
  • value: needle tested with SameValueZero equality.
  • Returns: greatest matching index, or -1 when absent.
1

Last duplicate slot

Within a plateau of equal numbers the answer hugs the right edge—here index 3 before 30 resumes.

javascript
import sortedLastIndexOf from "lodash/sortedLastIndexOf";

const tiers = [10, 20, 20, 20, 30];
sortedLastIndexOf(tiers, 20);
// → 3
Try it Yourself
2

Value absent

Missing needles never masquerade as insertion hints—you still read -1.

javascript
import sortedLastIndexOf from "lodash/sortedLastIndexOf";

sortedLastIndexOf([1, 3, 5, 7], 4);
// → -1
Try it Yourself
3

Bracket with sortedIndexOf

Subtract first from last and add one to count identical entries without walking the span.

javascript
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
Try it Yourself

📋 _.sortedLastIndexOf vs _.sortedIndexOf vs _.lastIndexOf

APIDuplicate bandRequires 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

Sort

Stale ordering

Partial inserts or descending slices invalidate assumptions—resort or guard before searching.

Cmp

Insertion helpers

Do not confuse with _.sortedLastIndex(), which always emits an insertion fence even when the needle is missing.

Cnt

Off-by-one counts

Inclusive duplicate counts need + 1 after subtracting first from last index.

❓ FAQ

No. It performs read-only binary search like other sortedIndex* lookups.
-1—same contract as _.lastIndexOf and _.sortedIndexOf.
Yes. The algorithm assumes monotonic ordering with default comparisons; unsorted input yields arbitrary indexes.
sortedLastIndex returns an insertion boundary even when the needle is absent. sortedLastIndexOf only reports actual matches (or -1).
Use import sortedLastIndexOf from "lodash/sortedLastIndexOf" or require("lodash/sortedLastIndexOf") for tree-shaking friendly bundles.

Summary

Did you know?

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

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