# ORDER

The `ORDER` clause specifies the sort order of the records in a table.

To sort records, SurrealDB allows ordering on multiple fields and nested fields. Use the `ORDER` clause to specify a comma-separated list of field names that should be used to order the resulting records.

While not necessary to parse, the `ORDER` clause can be followed with `BY` to make queries more readable.

The `ASC` and `DESC` keywords can be used to specify whether results should be sorted in an ascending or descending manner. The `COLLATE` keyword can be used to use Unicode collation when ordering text in string values, ensuring that different cases, and different languages are sorted in a consistent manner. Finally, the `NUMERIC` can be used to correctly sort text which contains numeric values.

It is also worth noting that `COLLATE` can be used to order by lexical instead of unicode order. For example, 'á' comes after 'z' by default (Unicode sorting) but with `COLLATE` 'á' comes before 'z'.

_(since v3.2.5)_

Sort fields do not need to appear in the `SELECT` list. Sorting runs before projection, so you can order by a field that is omitted from the result:

```surql
-- Sort by `at` without returning it
SELECT event, subject FROM audit_log ORDER BY at;
```

`SPLIT` and `GROUP` still require their fields to appear in the selection.

## Syntax

```syntax title="Clause Syntax"
[ ORDER [ BY ] 
	@field [ COLLATE ] [ NUMERIC ] [ ASC | DESC ], ...
	| RAND()
]
```

## Examples

```surql
SELECT * FROM <table> ORDER BY <field> ASC;
```

```surql 
-- Order records randomly
SELECT * FROM <table> ORDER BY rand();

-- Order records descending by a single field
SELECT * FROM <table> ORDER BY <field> DESC;

-- Order records by multiple fields independently
SELECT * FROM <table> ORDER BY <field> ASC, <field2> DESC;

-- Order text fields with lexical collation instead of Unicode order
SELECT * FROM <table> ORDER BY <field> COLLATE ASC;

-- Order text fields with which include numeric values
SELECT * FROM <table> ORDER BY <field> NUMERIC ASC;

-- COLLATE and NUMERIC can be used together
SELECT * FROM <table> ORDER BY <field> COLLATE NUMERIC ASC;
```
