Lodash _.orderBy() method
What you’ll learn
- How
_.orderBy(collection, iteratees, orders)composes multi-column sorts. - Mixing ascending and descending tie-breakers without nested compares.
- How this differs from
sortByand when nativesortstill wins. - Try each example in the editor (
?tryit=1,2,3) with Lodash from a CDN.
Prerequisites
Skim _.map() so iteratee shorthand feels familiar before sorting by property paths.
- You understand ascending versus descending comparisons.
- You can open Try-it labs or run snippets locally.
Overview
_.orderBy packages what UX tables need—sort by score, then name, flip just one column to descending—without hand-writing comparator spaghetti.
Multi-key sorts
Primary column plus deterministic tie-breakers.
Per-column direction
Mix asc and desc in the orders array.
Non-destructive
Returns a fresh sorted array every call.
Syntax
_.orderBy(collection, [iteratees], [orders]) - collection: array or plain object Lodash can iterate.
- iteratees: functions, property names, or path arrays evaluated per element.
- orders: parallel array of
"asc"/"desc"flags (defaults fill as ascending). - Returns: new array sorted according to the iteratee ladder.
Sort rows by a numeric field
Rows reorder by pts ascending—leaderboards from lowest to highest.
import orderBy from "lodash/orderBy";
orderBy(
[
{ player: "Ava", pts: 12 },
{ player: "Bo", pts: 5 },
{ player: "Cy", pts: 8 }
],
["pts"],
["asc"]
);
// → Bo (5), Cy (8), Ava (12) Two-level sort: team then slot
Everything in team "a" floats above team "b"; ties sort by slot.
import orderBy from "lodash/orderBy";
orderBy(
[
{ team: "b", slot: 2 },
{ team: "a", slot: 9 },
{ team: "a", slot: 1 }
],
["team", "slot"],
["asc", "asc"]
);
// → a:1, a:9, b:2 Primitive array, descending
An empty iteratees array falls back to comparing elements directly—pair it with desc.
import orderBy from "lodash/orderBy";
orderBy([3, 1, 2], [], ["desc"]);
// → [3, 2, 1] 📋 _.orderBy vs sortBy, native sort
| API | Directions | Best when |
|---|---|---|
_.orderBy(collection, iteratees, orders) | Mixed asc/desc per iteratee | Tables needing multi-column mixed directions |
_.sortBy(collection, iteratees) | Ascending chain only | Simpler rankings without desc toggles |
array.sort(compareFn) | Custom comparator | Hot paths with zero deps or ultra-custom logic |
Pitfalls to avoid
Mixed-type chaos
Numbers versus strings compare oddly—normalize fields before sorting critical UX lists.
Locale-sensitive strings
Default comparisons may not match locale collation rules—reach for Intl.Collator when polish matters.
Huge arrays
Sorting allocates comparisons—profile before layering heavy iteratees on megabyte payloads.
❓ FAQ
Summary
- Purpose:
_.orderBy(collection, iteratees, orders)sorts by layered iteratees with explicit directions. - Contrast: prefer
sortBywhen everything stays ascending. - Next: Lodash _.partition(), Lodash _.map() (previous), or collection hub.
_.orderBy returns a new sorted array—your input collection is left untouched. Pass parallel arrays for iteratees and orders so the second column only breaks ties after the first resolves.
6 people found this page helpful
