Lodash _.orderBy() method

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

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 sortBy and when native sort still 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

javascript
_.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.
1

Sort rows by a numeric field

Rows reorder by pts ascending—leaderboards from lowest to highest.

javascript
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)
Try it Yourself
2

Two-level sort: team then slot

Everything in team "a" floats above team "b"; ties sort by slot.

javascript
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
Try it Yourself
3

Primitive array, descending

An empty iteratees array falls back to comparing elements directly—pair it with desc.

javascript
import orderBy from "lodash/orderBy";

orderBy([3, 1, 2], [], ["desc"]);
// → [3, 2, 1]
Try it Yourself

📋 _.orderBy vs sortBy, native sort

APIDirectionsBest when
_.orderBy(collection, iteratees, orders)Mixed asc/desc per iterateeTables needing multi-column mixed directions
_.sortBy(collection, iteratees)Ascending chain onlySimpler rankings without desc toggles
array.sort(compareFn)Custom comparatorHot paths with zero deps or ultra-custom logic

Pitfalls to avoid

Types

Mixed-type chaos

Numbers versus strings compare oddly—normalize fields before sorting critical UX lists.

Locale

Locale-sensitive strings

Default comparisons may not match locale collation rules—reach for Intl.Collator when polish matters.

Perf

Huge arrays

Sorting allocates comparisons—profile before layering heavy iteratees on megabyte payloads.

❓ FAQ

No—it clones into a sorted array and leaves your original reference intact.
Lodash sorts primarily by the first iteratee; later iteratees break ties in order.
sortBy always sorts ascending with a single comparator chain; orderBy adds per-column asc/desc flags.
Typically "asc", "desc", or equivalently "ascending"/"descending" strings Lodash normalizes.
Lodash converts object collections to arrays of values before sorting—same as other collection helpers.

Summary

Did you know?

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

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