> ## Documentation Index
> Fetch the complete documentation index at: https://developers.luccasoftware.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Sorting

> You can sort items of most collections thanks to the `sort` query parameter.

<Note>
  Without sorting instructions, most collections implement a default sorting strategy, as described in the [API Reference](../../api-reference/latest).
</Note>

It takes a comma-separated list of keys as values. Each key is generally named with the name of the property the sorting will be applied on, e.g.: `id`, `name`, etc...

Sorting direction (i.e. "ascending" vs "descending") is indicated through adding a prefix to the name of the property the sorting should be applied to:

| Direction  | Prefix                         | Example                                                                    |
| :--------- | :----------------------------- | :------------------------------------------------------------------------- |
| Descending | `"-"` (hyphen)                 | `/employees?sort=-familyName`: return employees by descending family name. |
| Ascending  | `"+"` (plus) or `""` (nothing) | `/leaves?sort=startsOn`: return leaves by ascending start date.            |

<Warning>
  When using the "+" character, then it must be URL encoded, i.e.: `"%2B"`.
</Warning>

<CodeGroup>
  ```bnf BNF grammar theme={null}
  <sort>                  ::= '-|+'? <property> ( ',' <property> )*
  <property>              ::= <strictString> ( '.' <strictString> )*
  <strictString>          ::= ( <char> | <digit> | '_' | '-' )+
  ```
</CodeGroup>

When multiple sorting keys are given, then they are applied in the same order than they are given. For example: `/employees?sort=familyName,givenName` returns all employees, sorted by ascending family name THEN by ascending given name. In this case, the "Bros" brothers are returned by ascending first name: 'Luigi (Bros)', then 'Mario (Bros)'.

*P.S.: Technically, Mario's and Luigi's family name should be "Mario", but it feels wrong for some reason.*
