Underscore.js _.sortedIndex() Method

Beginner
5 min read
Updated: Nov 2026
4 examples
Array

Definition

Uses a binary search to find the smallest index at which a value should be inserted to keep the array sorted (official Underscore docs). Stable Array helper — not deprecated.

Pass the required arguments shown below. Underscore array helpers usually return a new value — check whether the source array is mutated.

Syntax

Call _.sortedIndex. Start with this line.

Underscore
_.sortedIndex(array, value, [iteratee], [context])
CallWhat it does
_.sortedIndex(array, value, [iteratee], [context])Primary signature.
_.sortedIndex(...)Call with the arguments your case needs.

Load Underscore with import _ from "underscore" (or the UMD CDN build) so these helpers are available as _.

Library declarations
Underscore
_.sortedIndex(array, value, [iteratee], [context])

Parameters

ParameterDescription
arrayRequired. Already-sorted list
valueRequired. Value (or object) to place
iterateeOptional. Function or property name for ranking
contextOptional. this for the iteratee

Return Value

Return ValueDescription
*Insert index (0 … length)

Underscore.js Examples

Example 1 — Official: Find the Insert Index

Locate where 35 belongs in a sorted number list.

Underscore
_.sortedIndex([10, 20, 30, 40, 50], 35);
// → 3
Try It Yourself

How It Works

  1. Sorted input. Numbers ascend: 10 … 50.
  2. Binary search. Underscore halves the range until low meets high.
  3. Result 3. 35 belongs after 30 and before 40.
  4. No mutation. The array is unchanged until you splice.

Example 2 — Before Start and After End

Values outside the current range land at the edges.

Underscore
var nums = [10, 20, 30];

_.sortedIndex(nums, 5);   // → 0
_.sortedIndex(nums, 99);  // → 3
Try It Yourself

How It Works

  1. Too small. 5 is less than every item → index 0.
  2. Too large. 99 is greater than every item → index length (3).
  3. Always valid. You never get -1 for “not found.”
  4. Use case. Growing a sorted list from either end.

Example 3 — Official: Objects by Property

Pass a property name as the iteratee to rank objects.

Underscore
var stooges = [
  { name: "moe", age: 40 },
  { name: "curly", age: 60 }
];

_.sortedIndex(stooges, { name: "larry", age: 50 }, "age");
// → 1
Try It Yourself

How It Works

  1. Sorted by age. List is ordered 40, then 60.
  2. Iteratee "age". Ranks each object (and the new one) by that property.
  3. Larry at 50. Belongs between moe and curly → index 1.
  4. Functions work too. Pass function (x) { return x.age; } for custom ranking.

Example 4 — Insert While Keeping Order

Combine _.sortedIndex with splice to insert in place.

Underscore
var list = [10, 20, 30, 40];
var i = _.sortedIndex(list, 25);
list.splice(i, 0, 25);
// list → [10, 20, 25, 30, 40]
Try It Yourself

How It Works

  1. Find index. 25 belongs at index 2.
  2. Splice in. splice(i, 0, 25) inserts without removing items.
  3. Order kept. No full re-sort needed.
  4. Note. splice mutates — that step is yours, not Underscore’s.

Browser & Runtime Support

_.sortedIndex ships with Underscore.js (1.x). It runs anywhere Underscore runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install underscore.

✓ Stable · Underscore 1.x

_.sortedIndex()

Available in [email protected] (UMD / ESM). Works in evergreen browsers and current Node LTS when Underscore is loaded.

100% With Underscore 1.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
_.sortedIndex Underscore 1.x+

Bottom line: Safe for production once Underscore is on the page or in your bundle. Not a native browser API — load Underscore first. Prefer a pinned CDN version (e.g. [email protected]).

Frequently Asked Questions

_.sortedIndex(array, value) uses a binary search to find the smallest index where value can be inserted so an already-sorted array stays in order.
Yes. It assumes ascending sorted order. On an unsorted list the result can be wrong.
Pass a function or property name to compute the ranking value — useful for objects, e.g. 'age'.
No. It always returns a valid insert index from 0 to array.length — never -1.
_.indexOf finds where a value already is (or -1). _.sortedIndex finds where a value should go to keep sorted order.
No. It only returns an index. Use splice yourself if you want to insert.

Next: _.findIndex()

Find the first index where a predicate returns true.

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