﻿---
title: Sorting functions
description: PromQL sorting functions in Elasticsearch that sort instant-vector series by sample value or by label values.
url: https://www.elastic.co/docs/reference/query-languages/promql/functions/sorting
products:
  - Elasticsearch
applies_to:
  - Elastic Cloud Serverless: Generally available
  - Elastic Stack: Planned
---

# Sorting functions
These functions sort the series of an instant vector by sample value (`sort` and `sort_desc`) or by listed label values (`sort_by_label` and `sort_by_label_desc`). Ordering is observable only on instant queries. Range queries return the series in their input order, without applying the requested ordering.

## `sort`

<applies-to>Elastic Stack: Planned</applies-to>
Sorts instant-vector series by sample value in ascending order. NaN values are sorted last.
Ordering is observable only on instant queries. Range queries return the series in their input order, without applying the requested ordering.
**Return type**
`instant_vector`
**Parameters**
<definitions>
  <definition term="v (instant_vector)">
    Instant vector input.
  </definition>
</definitions>

**Example**
```
sort(http_requests_total)
```

<note>
  **Differences from Prometheus**A call below the top level of the query, such as `abs(sort(x))`, isn't supported yet. Prometheus applies the ordering there too. On a range query the ordering is discarded in both, but Prometheus returns the series ordered by their labels, where Elasticsearch returns them in input order.
</note>


## `sort_by_label`

<applies-to>Elastic Stack: Planned</applies-to> <applies-to>Elastic Cloud Serverless: Preview</applies-to>
Sorts instant-vector series by the values of the given labels in natural ascending order. Missing labels compare as the empty string.
Ordering is observable only on instant queries. Range queries return the series in their input order, without applying the requested ordering. Label comparison uses natural order, with sequences of digits compared numerically.
**Return type**
`instant_vector`
**Parameters**
<definitions>
  <definition term="v (instant_vector)">
    Instant vector input.
  </definition>
  <definition term="label (scalar)">
    Name of a label to sort by (repeatable).
  </definition>
</definitions>

**Example**
```
sort_by_label(http_requests_total, "job", "instance")
```

<note>
  **Differences from Prometheus**
  - A call below the top level of the query, such as `abs(sort_by_label(x, "job"))`, isn't supported yet. Prometheus applies the ordering there too.
  - When the requested labels are equal, ties are broken by series identity, approximating Prometheus `labels.Compare`. The order can differ in edge cases, such as a series missing a label or labels of non-string types.
  - On a range query the ordering is discarded in both, but Prometheus returns the series ordered by their labels, where Elasticsearch returns them in input order.
  - Sequences of digits that are numerically equal but differ in leading zeros (for example, `item1` and `item01`), and empty label values, get a stable order rather than Prometheus's unstable natural sort order.
  - Sequences of digits longer than about 20 digits are still compared numerically, where Prometheus falls back to text comparison.
</note>


## `sort_by_label_desc`

<applies-to>Elastic Stack: Planned</applies-to> <applies-to>Elastic Cloud Serverless: Preview</applies-to>
Sorts instant-vector series by the values of the given labels in natural descending order. Missing labels compare as the empty string.
Ordering is observable only on instant queries. Range queries return the series in their input order, without applying the requested ordering. Label comparison uses natural order, with sequences of digits compared numerically.
**Return type**
`instant_vector`
**Parameters**
<definitions>
  <definition term="v (instant_vector)">
    Instant vector input.
  </definition>
  <definition term="label (scalar)">
    Name of a label to sort by (repeatable).
  </definition>
</definitions>

**Example**
```
sort_by_label_desc(http_requests_total, "job", "instance")
```

<note>
  **Differences from Prometheus**
  - A call below the top level of the query, such as `abs(sort_by_label(x, "job"))`, isn't supported yet. Prometheus applies the ordering there too.
  - When the requested labels are equal, ties are broken by series identity, approximating Prometheus `labels.Compare`. The order can differ in edge cases, such as a series missing a label or labels of non-string types.
  - On a range query the ordering is discarded in both, but Prometheus returns the series ordered by their labels, where Elasticsearch returns them in input order.
  - Sequences of digits that are numerically equal but differ in leading zeros (for example, `item1` and `item01`), and empty label values, get a stable order rather than Prometheus's unstable natural sort order.
  - Sequences of digits longer than about 20 digits are still compared numerically, where Prometheus falls back to text comparison.
</note>


## `sort_desc`

<applies-to>Elastic Stack: Planned</applies-to>
Sorts instant-vector series by sample value in descending order. NaN values are sorted last.
Ordering is observable only on instant queries. Range queries return the series in their input order, without applying the requested ordering.
**Return type**
`instant_vector`
**Parameters**
<definitions>
  <definition term="v (instant_vector)">
    Instant vector input.
  </definition>
</definitions>

**Example**
```
sort_desc(http_requests_total)
```

<note>
  **Differences from Prometheus**A call below the top level of the query, such as `abs(sort(x))`, isn't supported yet. Prometheus applies the ordering there too. On a range query the ordering is discarded in both, but Prometheus returns the series ordered by their labels, where Elasticsearch returns them in input order.
</note>