Lodash _.sortBy() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Collection

Definition

Creates an array of elements sorted in ascending order by running each element through one or more iteratees. Performs a stable sort. Part of Lodash 4.x Collection (since 0.1.0).

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

Syntax

Call _.sortBy on a collection. Start with this line.

Lodash
_.sortBy(collection, [iteratees=[_.identity]])
CallWhat it does
_.sortBy(collection, [iteratees=[_.identity]])Primary signature.
_.sortBy(collection)Call with a collection argument.

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

Library declarations
Lodash
_.sortBy(collection, [iteratees=[_.identity]])

Parameters

ParameterDescription
collectionOptional. Array or object (*nullish → [])
iterateesOptional. Sort keys; default identity; may be many

Return Value

Return ValueDescription
*New ascending sorted array

Lodash Examples

Example 1 — Official: Multi-Key Sort

Sort by user first, then by age.

Lodash
var users = [
  { user: "fred",   age: 48 },
  { user: "barney", age: 36 },
  { user: "fred",   age: 30 },
  { user: "barney", age: 34 }
];

_.sortBy(users, ["user", "age"]);
// → barney/34, barney/36, fred/30, fred/48
Try It Yourself

How It Works

  1. Primary key. "user" puts all barneys before freds.
  2. Secondary key. "age" sorts within each name.
  3. Ascending. Younger ages come first inside a name group.
  4. New array. users itself is unchanged.

Example 2 — Official: Function Iteratee

Sort by user only — equal names keep original relative order (stable).

Lodash
var users = [
  { user: "fred",   age: 48 },
  { user: "barney", age: 36 },
  { user: "fred",   age: 30 },
  { user: "barney", age: 34 }
];

_.sortBy(users, [function (o) { return o.user; }]);
// → barney/36, barney/34, fred/48, fred/30
Try It Yourself

How It Works

  1. One key. Only user participates in ordering.
  2. Stable ties. Both barneys keep source order: 36 before 34.
  3. Same for freds. 48 appears before 30 because that was the original order.
  4. Shorthand. _.sortBy(users, "user") is equivalent here.

Example 3 — Sort by a Single Property

Property shorthand is the most common form.

Lodash
var users = [
  { user: "fred",   age: 48 },
  { user: "barney", age: 36 },
  { user: "fred",   age: 30 }
];

_.sortBy(users, "age");
// → fred/30, barney/36, fred/48
Try It Yourself

How It Works

  1. Shorthand. "age" reads each object’s age.
  2. Ascending. 30 → 36 → 48.
  3. Names ignored. Only the iteratee matters.
  4. Safe. Original users order is preserved in memory.

Example 4 — Sort Plain Numbers

With no iteratee, identity sorts the values themselves.

Lodash
_.sortBy([3, 1, 2]);
// → [1, 2, 3]
Try It Yourself

How It Works

  1. Default iteratee. Identity uses each value as its own key.
  2. Ascending. Numbers order as 1, 2, 3.
  3. New array. Source [3, 1, 2] is not mutated.
  4. Strings. Identity also works for string lists (lexicographic).

Example 5 — Empty and Nullish

Nothing to sort → empty array.

Lodash
_.sortBy([], "age");
// → []

_.sortBy(null, "age");
// → []
Try It Yourself

How It Works

  1. Empty array. No elements → [].
  2. null. Lodash short-circuits and returns [].
  3. Safe. No throw — handy for optional API data.
  4. Guard. Still check upstream if empty is an error for your app.

Browser & Runtime Support

_.sortBy is a Lodash 4.x Collection helper (since 0.1.0). It runs anywhere Lodash runs — modern browsers (via CDN or bundle) and Node.js / Bun / Deno with npm install lodash.

✓ Stable · Lodash 0.1.0+

_.sortBy()

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

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

Frequently Asked Questions

_.sortBy(collection, [iteratees]) returns a new array sorted ascending by one or more iteratees. The sort is stable — equal keys keep their original relative order.
No. It returns a new array. The source collection is left unchanged.
sortBy is always ascending. _.orderBy lets you set asc/desc per iteratee when you need mixed directions.
Yes. Pass an array of iteratees, e.g. _.sortBy(users, ["user", "age"]). Earlier iteratees have higher priority.
_.sortBy(null) returns []. Lodash treats nullish collections as empty.
Use import sortBy from "lodash/sortBy" or require("lodash/sortBy") for tree-shaking. Full build: import _ from "lodash" then _.sortBy.

Next: Collection hub

Browse every Lodash collection method in one place.

Collection methods →

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