Basic field sorting
Return records alphabetically or chronologically.
Prisma · Pipe
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.
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.
prisma.<model>.findMany({ orderBy: { <field>: "asc" | "desc" } })| Name | Type | Description |
|---|---|---|
orderBy | XOR<Enumerable<ModelOrderByInput>, ModelOrderByInput> | A field-to-direction object or an array of such objects; directions are asc or desc. |
A list of records ordered according to the supplied criteria.
orderBy to Prisma Client queries to sort the returned records.{ title: "asc" }.asc for ascending order.desc for descending order.const posts = await prisma.post.findMany({
orderBy: [
{ role: "desc" },
{ name: "asc" },
],
});Fetches posts ordered by multiple fields, applying each criterion in array order.
Return records alphabetically or chronologically.
Apply a primary ordering and deterministic tie-breakers.
Order parent records using a related record's property.
Sort nested relation results independently within an include.
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.
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.
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" } }.
wherefindManyincludegroupByskiptake_count_relevance