Underscore.js _.sortBy() Method

Beginner
5 min read
Updated: Nov 2026
4 examples
Collection

Definition

Returns a stably sorted copy of the list, ranked in ascending order by running each value through an iteratee (official Underscore docs). The iteratee may also be a property name string. Stable Collection helper — not deprecated.

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

Syntax

Call _.sortBy. Start with this line.

Underscore
_.sortBy(list, iteratee, [context])
CallWhat it does
_.sortBy(list, iteratee, [context])Primary signature.
_.sortBy(...)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
_.sortBy(list, iteratee, [context])

Parameters

ParameterDescription
listRequired. Collection to sort
iterateeRequired. Criterion function or property name
contextOptional. this for the iteratee

Return Value

Return ValueDescription
*New ascending array (stable)

Underscore.js Examples

Example 1 — Official: Sort by Function

Rank numbers by Math.sin.

Underscore
_.sortBy([1, 2, 3, 4, 5, 6], function (num) {
  return Math.sin(num);
});
// → [5, 4, 6, 3, 1, 2]
Try It Yourself

How It Works

  1. Iteratee computes Math.sin(num) for each value.
  2. Ascending sort by that criterion (not by the number itself).
  3. Result is a new array: [5, 4, 6, 3, 1, 2].
  4. Source unchanged — always assign the return value.

Example 2 — Official: Property Shorthand

Sort objects by a string property name.

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

_.sortBy(stooges, "name");
// → curly, larry, moe
Try It Yourself

How It Works

  1. String iteratee "name" means “sort by obj.name”.
  2. Alphabetical ascending: curly → larry → moe.
  3. Same idea as _.sortBy(stooges, function (s) { return s.name; }).
  4. Need buckets? Prefer _.groupBy.

Example 3 — Original Stays Intact

Prove the source list is not mutated.

Underscore
var ages = [40, 60, 50];
var sorted = _.sortBy(ages);

console.log(ages);    // → [40, 60, 50]
console.log(sorted);  // → [40, 50, 60]
console.log(ages === sorted); // → false
Try It Yourself

How It Works

  1. Identity iteratee (omitted) sorts by the values themselves.
  2. New array returned — different reference from ages.
  3. Unlike Array.prototype.sort, which mutates in place.
  4. Objects inside are not deep-cloned — only order is new.

Example 4 — Descending Age

Ascending only — reverse or negate for descending.

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

_.sortBy(stooges, "age").reverse();
// → curly(60), larry(50), moe(40)

// or negate a numeric key:
_.sortBy(stooges, function (s) { return -s.age; });
// → same descending order
Try It Yourself

How It Works

  1. _.sortBy always ranks ascending by the criterion.
  2. .reverse() flips the already-sorted copy.
  3. Negate numbers so larger ages become “smaller” keys.
  4. Note: .reverse() mutates that copy — fine if you chain on the return value.

Browser & Runtime Support

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

_.sortBy()

Available in [email protected] (UMD / ESM). Stable ascending sort. 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
_.sortBy 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

_.sortBy(list, iteratee, [context]) returns a stably sorted copy of the list in ascending order by each value's iteratee result. Official: _.sortBy([1..6], Math.sin) → [5, 4, 6, 3, 1, 2].
No. It returns a new array. The source collection is left unchanged.
Yes. Equal criteria keep their original relative order — useful for multi-pass sorts.
Yes. Pass a string: _.sortBy(stooges, 'name') sorts by each object's name property.
_.sortBy is ascending only. Reverse the result, or negate a numeric criterion in the iteratee.
Array.sort mutates in place and defaults to string compare. _.sortBy returns a new array, sorts by your iteratee, and is stable.

Next: _.groupBy()

Split a collection into sets keyed by an iteratee result.

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