Lodash _.sortedLastIndexOf() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Array

Definition

Like _.lastIndexOf, except it performs a binary search on a sorted array. Returns the highest matching index, or -1 when the value is absent. Part of Lodash Array since 4.0.0 (stable — 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 _.sortedLastIndexOf on an array. Start with this line.

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

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

Library declarations
Lodash
_.sortedLastIndexOf(array, value)

Parameters

ParameterDescription
arrayRequired. Sorted ascending collection to search
valueRequired. Value to look up

Return Value

Return ValueDescription
*Highest matching index, or -1

Lodash Examples

Example 1 — Official: Highest Match Among Duplicates

Among equal values, return the rightmost index.

Lodash
_.sortedLastIndexOf([4, 5, 5, 5, 6], 5);
// → 3
Try It Yourself

How It Works

  1. Sorted source. Values ascend: 4, 5, 5, 5, 6.
  2. Search. Binary search looks for 5.
  3. Rightmost. Matches exist at 1, 2, 3 — return 3.
  4. Tip. Use _.sortedIndexOf for the leftmost match.

Example 2 — sortedLastIndexOf vs sortedIndexOf

Bracket a run of duplicates with both helpers.

Lodash
var arr = [4, 5, 5, 5, 6];

console.log(_.sortedIndexOf(arr, 5));     // 1
console.log(_.sortedLastIndexOf(arr, 5)); // 3
Try It Yourself

How It Works

  1. Left. sortedIndexOf → first 5 at index 1.
  2. Right. sortedLastIndexOf → last 5 at index 3.
  3. Band. Inclusive range [1, 3] holds all equals.
  4. Use. Slice or count that plateau without a full scan.

Example 3 — Missing Value Returns -1

Unlike sortedLastIndex, a miss is not an insert slot.

Lodash
_.sortedLastIndexOf([4, 5, 5, 5, 6], 3);
// → -1
Try It Yourself

How It Works

  1. Probe. 3 is not in the array.
  2. Confirm miss. Lodash returns -1.
  3. Contrast. _.sortedLastIndex(arr, 3) would return 0 (insert at front).
  4. Guard. Always check idx !== -1 before indexing.

Example 4 — Match at Either End

Unique edge values still resolve with binary search.

Lodash
var arr = [4, 5, 5, 5, 6];

console.log(_.sortedLastIndexOf(arr, 4)); // 0
console.log(_.sortedLastIndexOf(arr, 6)); // 4
Try It Yourself

How It Works

  1. First. Only one 4 → highest match is also 0.
  2. Last. Only one 6 → highest match is 4.
  3. Same as left. With a single occurrence, both helpers agree.
  4. Fast. Still O(log n), not a reverse linear walk.

Example 5 — Count a Duplicate Run

Subtract the two match bounds (inclusive) to get the count.

Lodash
var arr = [4, 5, 5, 5, 6];
var lo = _.sortedIndexOf(arr, 5);
var hi = _.sortedLastIndexOf(arr, 5);

console.log(lo, hi);          // 1 3
console.log(hi - lo + 1);     // 3
Try It Yourself

How It Works

  1. Bounds. Lowest match 1, highest match 3.
  2. Inclusive count. 3 - 1 + 1 = 3.
  3. Guard. If either helper returns -1, the value is missing.
  4. Use. Measure plateaus in sorted scores, ids, or timestamps.

Browser & Runtime Support

_.sortedLastIndexOf is a Lodash 4.x Array helper (since 4.0.0). It runs anywhere Lodash runs — modern browsers (via CDN or bundle) and Node.js / Bun / Deno with npm install lodash. Native engines have lastIndexOf, but not this binary-search variant by name.

✓ Stable · Lodash 4.0.0+

_.sortedLastIndexOf()

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

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

Frequently Asked Questions

_.sortedLastIndexOf(array, value) is like _.lastIndexOf, but uses a binary search on a sorted ascending array. It returns the highest index of a matching value, or -1 if the value is not present.
No. It is a read-only lookup. The array stays unchanged.
sortedIndexOf returns the lowest matching index. sortedLastIndexOf returns the highest. Pair them to measure a run of duplicates.
sortedLastIndex returns an insert boundary (always ≥ 0). sortedLastIndexOf returns a matching index or -1 when the value is missing.
Yes — ascending order. An unsorted array produces meaningless indexes because the binary search assumes sorted data.
Use import sortedLastIndexOf from "lodash/sortedLastIndexOf" or require("lodash/sortedLastIndexOf") so bundlers can tree-shake unused Lodash code. Full build: import _ from "lodash" then _.sortedLastIndexOf.

Next: _.sortedUniq()

Create a duplicate-free array from sorted consecutive values.

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