Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Prisma · Pipe

orderBy

Prisma Client's orderBy option sorts query results by one or more fields. It supports ascending or descending directions, multiple criteria, and ordering through relations.

How it works

Use orderBy in Prisma Client queries to control the ordering of returned records. Provide one object for a single criterion or an array for multiple criteria, with each field mapped to asc or desc; relation fields and nested relation lists can also be ordered.

Syntax

prisma.<model>.findMany({ orderBy: { <field>: "asc" | "desc" } })

Parameters

NameTypeDescription
orderByXOR<Enumerable<ModelOrderByInput>, ModelOrderByInput>A field-to-direction object or an array of such objects; directions are asc or desc.

Returns

A list of records ordered according to the supplied criteria.

Key points

  • Add orderBy to Prisma Client queries to sort the returned records.
  • The option accepts a field-to-direction object, such as { title: "asc" }.
  • Use asc for ascending order.
  • Use desc for descending order.
  • Relation fields can be used as ordering criteria through nested objects.
  • Related fields provide another way to determine the order of top-level records.

Example: Sort records by multiple fields

const posts = await prisma.post.findMany({
  orderBy: [
    { role: "desc" },
    { name: "asc" },
  ],
});

Fetches posts ordered by multiple fields, applying each criterion in array order.

When to use it

Basic field sorting

Return records alphabetically or chronologically.

Multi-field sorting

Apply a primary ordering and deterministic tie-breakers.

Relation-based sorting

Order parent records using a related record's property.

Nested list sorting

Sort nested relation results independently within an include.

Interview questions

QWhat does orderBy do, and how can multiple sort criteria be specified?

orderBy sorts records returned by Prisma Client. A single object expresses one or more field criteria, while an array makes the ordering criteria explicit in sequence; each direction is asc or desc.

Follow-up: How would you sort by a related record? Use a nested relation object in orderBy, such as ordering posts through a property of their author.

QHow do ascending and descending ordering work?

For a normal query, use a field-direction mapping such as { email: "desc" }. For multiple criteria, pass an array such as [{ role: "desc" }, { name: "asc" }].

Follow-up: Can nested relation lists be sorted? Yes; put orderBy inside the relation's nested query, for example in an include for a user's posts.

QWhat restrictions apply when combining orderBy with groupBy()?

groupBy() has additional ordering constraints: fields ordered directly must be present in by, and aggregate ordering is supported. When pagination uses skip or take, orderBy is required.

Follow-up: How can a group be ordered by an aggregate? Use an aggregate ordering object, such as { _count: { city: "desc" } }.

  • where
  • findMany
  • include
  • groupBy
  • skip
  • take
  • _count
  • _relevance

Source: Filtering and sorting (Prisma ORM v7). Checked 2026-10-16.