Lodash _.sortBy() method

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

What you’ll learn

  • How _.sortBy(collection, [iteratees]) performs stable ascending sorting.
  • Single-key and multi-key sorting with iteratee functions or property paths.
  • When to switch to orderBy for descending control.
  • Try each example in the editor (?tryit=1, 2, 3) with Lodash from a CDN.

Prerequisites

Read _.orderBy() when you need mixed ascending and descending directions.

  • You understand compare-key sorting (e.g., age first, then name).
  • You can open Try-it labs or run snippets locally.

Overview

_.sortBy keeps sorting readable and stable—ideal for table views, report exports, and leaderboard prep where tie order should remain predictable.

Ascending by default

All iteratees sort ascending; use orderBy for descending directions.

Stable ordering

Equal keys keep input order, preventing jittery UI tie-breaks.

Non-mutating

Returns a new array so original data can still power other views.

Syntax

javascript
_.sortBy(collection, [iteratees])
  • collection: array or plain object values to sort.
  • iteratees: function, property name/path, or array of iteratees for chained keys.
  • Returns: new sorted array; source collection is untouched.
1

Sort numbers ascending

Use identity-style keys for quick ascending ranking lists.

javascript
import sortBy from "lodash/sortBy";

sortBy([40, 10, 30, 20]);
// → [10, 20, 30, 40]
Try it Yourself
2

Sort records by one property

Property-name iteratees keep code compact for common table sorts.

javascript
import sortBy from "lodash/sortBy";

sortBy(
  [
    { id: 1, name: "Zed", score: 70 },
    { id: 2, name: "Ana", score: 92 },
    { id: 3, name: "Bob", score: 85 }
  ],
  "name"
);
// → Ana, Bob, Zed
Try it Yourself
3

Sort by multiple iteratees

Use iteratee arrays for deterministic tie-breaking while staying ascending per key.

javascript
import sortBy from "lodash/sortBy";

sortBy(
  [
    { team: "A", score: 90, name: "Sam" },
    { team: "A", score: 90, name: "Alex" },
    { team: "B", score: 80, name: "Rita" }
  ],
  ["score", "name"]
);
// → score asc, then name asc
Try it Yourself

📋 _.sortBy vs orderBy, native sort

APIOrder controlBest when
_.sortBy(collection, iteratees)Ascending onlySimple stable sorts with concise iteratees
_.orderBy(collection, iteratees, orders)Asc/desc per keyMixed-direction analytics grids
array.sort(compareFn)Custom compare functionFine-grained control without Lodash

Pitfalls to avoid

Direction

Expecting descending

sortBy never takes direction flags; move to orderBy for descending fields.

Data

Mixed key types

Normalize numeric strings and nullish values first so sort intent is obvious and reproducible.

Perf

Repeated resorting

Cache expensive iteratee projections in hot loops instead of sorting huge lists every render.

❓ FAQ

No. Lodash returns a new sorted array and leaves the original collection unchanged.
sortBy itself is ascending-only; use _.orderBy for per-field asc/desc control.
Yes in Lodash 4.x: equal iteratee results keep original relative ordering.
Yes. Object values are collected and returned as a sorted array.
They usually drift toward the end with ascending semantics; normalize keys explicitly when order must be strict.

Summary

Did you know?

_.sortBy is stable in Lodash 4.x: entries with equal iteratee results keep their original relative order, which helps preserve UI tie-breaking predictability.

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