order()

Signature

order(): Set<A>

order(…​, ordering: Ordering<A>[]): Set<A>

Description

The order() method creates a sorted Set by applying the ordering to criteria to the source Set.

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:
asc(.<field>): Order the result in ascending order for the provided fields.
desc(.<field>): Order the result in descending order for the provided fields.

Return value

Type Description

Set

New Set with members in requested order.

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!