set.order()
Sort a Set's elements.
Description
You define each sorting criterion by wrapping asc()
(ascending) or desc()
(descending) around a field accessor or
a read-only anonymous function.
The first criterion has the highest sorting priority, with priority decreasing for each subsequent criterion.
If order()
is the last value in an expression, the first page of the new
Set is returned. See Pagination.
The calling Set remains unchanged.
Parameters
Parameter | Type | Required | Description |
---|---|---|---|
ordering |
One or more sorting criteria, separated by commas. Each criterion is a field accessor or
read-only anonymous function, optionally
wrapped in If neither The anonymous function is passed each Set element as an argument. For document Sets, each Set element is a Document. |
Examples
Sort fields in ascending order
// Sort `Customer` collection documents by `name`
// in ascending order (default).
Customer.all().order(.name) { name, email }
{
data: [
{
name: "Alice Appleseed",
email: "alice.appleseed@example.com"
},
{
name: "Bob Brown",
email: "bob.brown@example.com"
},
{
name: "Carol Clark",
email: "carol.clark@example.com"
}
]
}
Sort fields in descending order
// Sort `Customer` collection documents by `name`
// in descending order.
Customer.all().order(desc(.name)) { name, email }
{
data: [
{
name: "Carol Clark",
email: "carol.clark@example.com"
},
{
name: "Bob Brown",
email: "bob.brown@example.com"
},
{
name: "Alice Appleseed",
email: "alice.appleseed@example.com"
}
]
}
Sort fields using multiple arguments
// Sort `Customer` collection documents by:
// - Ascending `name` then...
// - Ascending `address.street`.
Customer.all().order(.name, .address.street) { name, address { street }, email }
{
data: [
{
name: "Alice Appleseed",
address: {
street: "87856 Mendota Court"
},
email: "alice.appleseed@example.com"
},
{
name: "Bob Brown",
address: {
street: "72 Waxwing Terrace"
},
email: "bob.brown@example.com"
},
{
name: "Carol Clark",
address: {
street: "5 Troy Trail"
},
email: "carol.clark@example.com"
}
]
}
Sort fields using an anonymous function
In addition to using field accessors, you can use a read-only anonymous function as a sorting criterion:
// Sort Customer collection documents by prioritizing
// those where the email contains "example.com".
Customer.all().order(asc(
(doc) => if (doc.email.includes("example.com")) {
// Prioritize these documents.
// Lower numbers (`0`)` appear first when sorted
// in ascending order.
0
// After that, sort the remaining documents by the
// length of their email.
} else {
doc.email.length
}
)) { name, email }
{
data: [
{
name: "Alice Appleseed",
email: "alice.appleseed@example.com"
},
{
name: "Bob Brown",
email: "bob.brown@example.com"
},
{
name: "Carol Clark",
email: "carol.clark@example.com"
},
{
name: "Jane Doe",
email: "12-fake@fauna.com"
},
{
name: "John Doe",
email: "123-fake@fauna.com"
}
]
}
Is this article helpful?
Tell Fauna how the article can be improved:
Visit Fauna's forums
or email docs@fauna.com
Thank you for your feedback!