order()
Lazy loading: |
Description
The order()
method takes one or more Ordering
objects to sort
a Set. An Ordering object is created by using the asc()
or
desc()
function with a field accessor:
-
asc()
: sort by the field in ascending order -
desc()
: sort by the field in descending order
The first ordering parameter has the highest sorting priority and priority decreases with each subsequent parameter.
If order()
is the last value in a query, the first page of the new
Set is returned.
The source set isn’t changed.
Parameters
Parameter | Type | Required | Description |
---|---|---|---|
ordering |
Ordering[] |
Ordering to apply to Set values. One or more Ordering objects. ordering objects: |
Examples
Sort in ascending order
// Sort `Customer` collection documents by `lastName`
// in ascending order (default).
Customer.all().order(.lastName) { firstName, lastName, email }
{
data: [
{
firstName: "Alice",
lastName: "Appleseed",
email: "alice.appleseed@example.com"
},
{
firstName: "Bob",
lastName: "Brown",
email: "bob.brown@example.com"
},
{
firstName: "Carol",
lastName: "Clark",
email: "carol.clark@example.com"
}
]
}
Sort in descending order
// Sort `Customer` collection documents by `lastName`
// in ascending order (default).
Customer.all().order(desc(.lastName)) { firstName, lastName, email }
{
data: [
{
firstName: "Carol",
lastName: "Clark",
email: "carol.clark@example.com"
},
{
firstName: "Bob",
lastName: "Brown",
email: "bob.brown@example.com"
},
{
firstName: "Alice",
lastName: "Appleseed",
email: "alice.appleseed@example.com"
}
]
}
Sort using multiple arguments
// Sort `Customer` collection documents by:
// - Ascending `lastName` then...
// - Ascending `firstName`.
Customer.all().order(.lastName, .firstName) { firstName, lastName, email }
{
data: [
{
firstName: "Alice",
lastName: "Appleseed",
email: "alice.appleseed@example.com"
},
{
firstName: "Bob",
lastName: "Brown",
email: "bob.brown@example.com"
},
{
firstName: "Carol",
lastName: "Clark",
email: "carol.clark@example.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!