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 the People documents by last name, in default ascending order:

Programmer.all().order(.last) { first, last, age }
{
  data: [
    {
      first: "Stephen",
      last: "Cook",
      age: 83
    },
    {
      first: "Tim",
      last: "Cook",
      age: 62
    },
    {
      first: "Grace",
      last: "Hopper",
      age: 116
    },
    {
      first: "Leslie",
      last: "Lamport",
      age: 81
    },
    {
      first: "Marvin",
      last: "Minsky",
      age: 95
    },
    {
      first: "Alan",
      last: "Perlis",
      age: 100
    },
    {
      first: "Alan",
      last: "Turing",
      age: 110
    }
  ]
}

 

Sort the People documents by age, in descending order:

Programmer.all().order(desc(.age)) {first, last, age}
{
  data: [
    {
      first: "Grace",
      last: "Hopper",
      age: 116
    },
    {
      first: "Alan",
      last: "Turing",
      age: 110
    },
    {
      first: "Alan",
      last: "Perlis",
      age: 100
    },
    {
      first: "Marvin",
      last: "Minsky",
      age: 95
    },
    {
      first: "Stephen",
      last: "Cook",
      age: 83
    },
    {
      first: "Leslie",
      last: "Lamport",
      age: 81
    },
    {
      first: "Tim",
      last: "Cook",
      age: 62
    }
  ]
}

 

Sort the People documents by last name and age, in default ascending order of last name and age, projecting first name, last name, and age:

Programmer.all().order(.last, .age) {first, last, age}
{
  data: [
    {
      first: "Tim",
      last: "Cook",
      age: 62
    },
    {
      first: "Stephen",
      last: "Cook",
      age: 83
    },
    {
      first: "Grace",
      last: "Hopper",
      age: 116
    },
    {
      first: "Leslie",
      last: "Lamport",
      age: 81
    },
    {
      first: "Marvin",
      last: "Minsky",
      age: 95
    },
    {
      first: "Alan",
      last: "Perlis",
      age: 100
    },
    {
      first: "Alan",
      last: "Turing",
      age: 110
    }
  ]
}

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!