Lodash _.sortBy() method
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
orderByfor 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
_.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.
Sort numbers ascending
Use identity-style keys for quick ascending ranking lists.
import sortBy from "lodash/sortBy";
sortBy([40, 10, 30, 20]);
// → [10, 20, 30, 40] Sort records by one property
Property-name iteratees keep code compact for common table sorts.
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 Sort by multiple iteratees
Use iteratee arrays for deterministic tie-breaking while staying ascending per key.
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 📋 _.sortBy vs orderBy, native sort
| API | Order control | Best when |
|---|---|---|
_.sortBy(collection, iteratees) | Ascending only | Simple stable sorts with concise iteratees |
_.orderBy(collection, iteratees, orders) | Asc/desc per key | Mixed-direction analytics grids |
array.sort(compareFn) | Custom compare function | Fine-grained control without Lodash |
Pitfalls to avoid
Expecting descending
sortBy never takes direction flags; move to orderBy for descending fields.
Mixed key types
Normalize numeric strings and nullish values first so sort intent is obvious and reproducible.
Repeated resorting
Cache expensive iteratee projections in hot loops instead of sorting huge lists every render.
❓ FAQ
Summary
- Purpose:
_.sortBy(collection, iteratees)gives stable ascending ordering on one or more keys. - Contrast: choose
orderBywhen direction per key matters. - Next: Collection method index, Lodash _.some() (previous), or collection hub.
_.sortBy is stable in Lodash 4.x: entries with equal iteratee results keep their original relative order, which helps preserve UI tie-breaking predictability.
6 people found this page helpful
