Skip to content

orderby / sortby

Description

The orderby command sorts query results in ascending or descending order based on one or more expressions. It supports multiple sort keys, allowing you to order by several fields sequentially.

The command has several aliases, orderby, sortby, order by, and sort by, that behave identically.

Note

Sorting is limited to 10,000 values. Beyond that limit, order is not guaranteed.

Syntax

(orderby|sortby|order by|sort by) <expression> [(asc|desc)] , ...

Example 1

Use case: Sort users alphabetically and by activity count

Suppose you have a list of users with their respective activity counts, and you want to sort them alphabetically.

Example data

{ "user": "Maria", "activity_count": 3 },
{ "user": "Chris", "activity_count": 8 },
{ "user": "Dave",  "activity_count": 15 }

Example query

orderby user asc

Example output

{ "user": "Chris", "activity_count": 8 },
{ "user": "Dave",  "activity_count": 15 },
{ "user": "Maria", "activity_count": 3 }

Example 2

Sorting alphabetically (asc) arranges values from A to Z. To sort by activity count instead, you can provide an expression and direction:

Example query

orderby activity_count desc

Example output

{ "user": "Chris", "activity_count": 8 },
{ "user": "Dave",  "activity_count": 5 },
{ "user": "Maria", "activity_count": 3 }

Tip

When sorting by numeric fields, ensure you cast them to number to avoid lexicographic sorting:

orderby my_num_field:number desc