order()

Get a new Array by sorting this Array.

Signature

order(): Array<A>

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

Description

The order() method creates a Array by applying an Ordering to the values of this Array, and returns the new Array.

An Ordering is a method that takes one or more Ordering objects to sort a Array. 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 entry in the Ordering has the highest sorting priority, with priority decreasing with each following entry.

If order() is the last value in a query, the first page of the new Array is returned.

The source array isn’t changed.

Parameters

Parameter Type Required Description

ordering

Ordering

Ordering to apply to Array values. One or more Ordering objects.

Ordering objects:
asc(<field>): Order the result in ascending order for the provided field.
desc(<field>): Order the result in descending order for the provided field.

Return value

Type Description

Array

Array with elements in ordering order.

Examples

Order the array elements returned by the function in ascending order.

[3, 1, 2, 4].order(asc(v => v))
[
  1,
  2,
  3,
  4
]

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!