set.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 `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 in descending order

// Sort `Customer` collection documents by `name`
// in ascending order (default).
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 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"
    }
  ]
}

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!