Skip to main content

Sorting

Sort results by system fields, custom data fields, or aggregated array values.

note

By default, all queries are sorted by createdAt: desc (newest first). Override with explicit orderBy parameters.

System Field Sorting

Sort by built-in fields available on all entities:

query GetProductsSorted {
products(data: {
orderBy: [
{ field: createdAt, direction: desc }
{ field: id, direction: asc }
]
}) {
edges {
node { id createdAt data { name price } }
}
}
}

Available system fields: id, createdAt, updatedAt, publishedAt

JSON Path Sorting

Sort by any field in your data using dot notation with type casting:

query GetProductsByPrice {
products(data: {
orderBy: [
{ data: { path: "price", direction: "desc", type: "float" } }
{ data: { path: "name", direction: "asc", type: "text" } }
]
}) {
edges {
node { data { name price } }
}
}
}

Type Casting

Proper type ensures correct sort order:

TypeUsage
textAlphabetical (default)
intInteger numbers
floatDecimal numbers
booleantrue/false
timestampDate/time values

Without type casting, numbers sort lexicographically ("9" > "10"). Use int or float for numeric sorting.

Nested Paths

Use dot notation for nested fields:

{ data: { path: "specs.weight", direction: "asc", type: "float" } }
{ data: { path: "settings.ui.theme.color", direction: "desc", type: "text" } }

Array Aggregation Sorting

Sort by aggregated values from array fields using [*] wildcard with an aggregation function:

query GetProductsByMinPrice {
products(data: {
orderBy: [
{ data: { path: "prices[*]", direction: "asc", type: "float", aggregation: min } }
]
}) {
edges { node { data { name prices } } }
}
}

Sort by the lowest price in each product's price array.

Mixed Sorting

Combine system fields and custom data fields in a single orderBy:

query GetProductsAdvanced {
products(data: {
orderBy: [
{ data: { path: "featured", direction: "desc", type: "boolean" } }
{ data: { path: "price", direction: "asc", type: "float" } }
{ field: createdAt, direction: desc }
]
}) {
edges {
node { data { name featured price } createdAt }
}
}
}