# `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

```dataprime
(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

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

### Example query

```dataprime
orderby user asc
```

### Example output

```json
{ "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

```dataprime
orderby activity_count desc
```

### Example output

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

Note

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

```dataprime
orderby my_num_field:number desc
```
