4.7 KiB
title | description | aliases | menu | weight | flux/v0.x/tags | related | introduced | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
sort() function | The `sort()` function orders the records within each table. |
|
|
102 |
|
|
0.7.0 |
The sort()
function orders the records within each table.
One output table is produced for each input table.
The output tables will have the same schema as their corresponding input tables.
Sorting with null values
When sorting, null
values will always be first.
When desc: false
, nulls are less than every other value.
When desc: true
, nulls are greater than every value.
sort(columns: ["_value"], desc: false)
Parameters
columns
List of columns by which to sort.
Sort precedence is determined by list order (left to right).
Default is ["_value"]
.
desc
Sort results in descending order.
Default is false
.
tables
Input data.
Default is piped-forward data (<-
).
Examples
{{% flux/sample-example-intro %}}
Sort values in ascending order
import "sampledata"
sampledata.int()
|> sort()
{{< expand-wrapper >}} {{% expand "View input and output" %}} {{< flex >}} {{% flex-content %}}
Input data
{{% flux/sample "int" %}}
{{% /flex-content %}} {{% flex-content %}}
Output data
_time | tag | _value |
---|---|---|
2021-01-01T00:00:00Z | t1 | -2 |
2021-01-01T00:00:50Z | t1 | 4 |
2021-01-01T00:00:20Z | t1 | 7 |
2021-01-01T00:00:10Z | t1 | 10 |
2021-01-01T00:00:40Z | t1 | 15 |
2021-01-01T00:00:30Z | t1 | 17 |
_time | tag | _value |
---|---|---|
2021-01-01T00:00:20Z | t2 | -3 |
2021-01-01T00:00:50Z | t2 | 1 |
2021-01-01T00:00:10Z | t2 | 4 |
2021-01-01T00:00:40Z | t2 | 13 |
2021-01-01T00:00:00Z | t2 | 19 |
2021-01-01T00:00:30Z | t2 | 19 |
{{% /flex-content %}} | ||
{{< /flex >}} | ||
{{% /expand %}} | ||
{{< /expand-wrapper >}} |
Sort values in descending order
import "sampledata"
sampledata.int()
|> sort(desc: true)
{{< expand-wrapper >}} {{% expand "View input and output" %}} {{< flex >}} {{% flex-content %}}
Input data
{{% flux/sample "int" %}}
{{% /flex-content %}} {{% flex-content %}}
Output data
_time | tag | _value |
---|---|---|
2021-01-01T00:00:30Z | t1 | 17 |
2021-01-01T00:00:40Z | t1 | 15 |
2021-01-01T00:00:10Z | t1 | 10 |
2021-01-01T00:00:20Z | t1 | 7 |
2021-01-01T00:00:50Z | t1 | 4 |
2021-01-01T00:00:00Z | t1 | -2 |
_time | tag | _value |
---|---|---|
2021-01-01T00:00:00Z | t2 | 19 |
2021-01-01T00:00:30Z | t2 | 19 |
2021-01-01T00:00:40Z | t2 | 13 |
2021-01-01T00:00:10Z | t2 | 4 |
2021-01-01T00:00:50Z | t2 | 1 |
2021-01-01T00:00:20Z | t2 | -3 |
{{% /flex-content %}} | ||
{{< /flex >}} | ||
{{% /expand %}} | ||
{{< /expand-wrapper >}} |
Sort by multiple columns
import "sampledata"
sampledata.int()
|> sort(columns: ["tag", "_value"])
{{< expand-wrapper >}} {{% expand "View input and output" %}} {{< flex >}} {{% flex-content %}}
Input data
{{% flux/sample "int" %}}
{{% /flex-content %}} {{% flex-content %}}
Output data
_time | tag | _value |
---|---|---|
2021-01-01T00:00:00Z | t1 | -2 |
2021-01-01T00:00:50Z | t1 | 4 |
2021-01-01T00:00:20Z | t1 | 7 |
2021-01-01T00:00:10Z | t1 | 10 |
2021-01-01T00:00:40Z | t1 | 15 |
2021-01-01T00:00:30Z | t1 | 17 |
_time | tag | _value |
---|---|---|
2021-01-01T00:00:20Z | t2 | -3 |
2021-01-01T00:00:50Z | t2 | 1 |
2021-01-01T00:00:10Z | t2 | 4 |
2021-01-01T00:00:40Z | t2 | 13 |
2021-01-01T00:00:00Z | t2 | 19 |
2021-01-01T00:00:30Z | t2 | 19 |
{{% /flex-content %}} | ||
{{< /flex >}} | ||
{{% /expand %}} | ||
{{< /expand-wrapper >}} |