For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /reference/fp/orderBy.md.

orderBy (Functional Programming) es-toolkit

Creates a function that sorts objects by criteria and sort orders. Use it with pipe.

const result = pipe(array, orderBy(criteria, orders));
Info

Prefer the original es-toolkit orderBy in ordinary code. Use this fp variant when composing transformations with pipe.

Usage

orderBy sorts the piped array by each criterion in order. Each order controls whether the matching criterion is sorted ascending or descending.

import { orderBy, pipe } from 'es-toolkit/fp';

const users = [
  { name: 'a', age: 2 },
  { name: 'b', age: 1 },
];

pipe(users, orderBy(['age'], ['asc'])); // => [{ name: 'b', age: 1 }, { name: 'a', age: 2 }]

Parameters

criteriaArray<((item: T) => unknown) | keyof T>required

The object keys and/or selector functions used for comparison.

ordersArray<'asc' | 'desc'>required

The sort order for each criterion.

Returns

A function that maps a readonly T[] to a new, sorted array.

(array: readonly T[]) => T[]
Source: es-toolkit

Re-exported verbatim from es-toolkit. Implementation, edge cases, and performance behavior are owned upstream. This page mirrors the documentation at the pinned version; the linked source is authoritative.