# Sort by highest value

**URL:** <https://community.influxdata.com/t/sort-by-highest-value/26030>\
**Category:** InfluxDB 2\
**Tags:** query\
**Created:** [August 5, 2022, 9:10pm UTC](https://community.influxdata.com/t/sort-by-highest-value/26030 "2022-08-05T21:10:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kevin\_Verreth](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/kevin_verreth/32/10691_2.png) [@kevin\_Verreth](https://community.influxdata.com/u/kevin_Verreth)\
**Post date:** [August 5, 2022, 9:10pm UTC](https://community.influxdata.com/t/sort-by-highest-value/26030/1 "2022-08-05T21:10:50Z")

</div>

Hi

I am trying to create a query to sort the last field of each measurement descending.

This is my query:

```auto
from(bucket: "iobroker")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "loxone.0.1990ece5-000b-a827-ffffb2ef6a95b230.value" or r["_measurement"] == "loxone.0.167f5741-037a-8b3d-ffffb2ef6a95b230.value" or r["_measurement"] =="loxone.0.1826dbac-0057-2552-ffffb2ef6a95b230.value")
  |> filter(fn: (r) => r["_field"] == "value")

```

This will show the output like this (in Grafana)

57  
1.23  
21.6

i wish to have it like:  
57  
21.6  
1.23

This is the raw data:

 ![image](https://us1.discourse-cdn.com/flex023/uploads/influxdata/original/2X/6/6db696344c6b4bcaa948f6185ebb00b45cbc4e8a.png)

I tried to use the sort and group functions but the results always come out weird. I cant seem to figure out what the correct syntax or functions are to accomplish this.

Kind regards  
Kevin

---

<div class="post-metadata">

**Author:** ![LordOfTheSnow](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/lordofthesnow/32/10705_2.png) [@LordOfTheSnow](https://community.influxdata.com/u/LordOfTheSnow)\
**Post date:** [August 7, 2022, 10:47am UTC](https://community.influxdata.com/t/sort-by-highest-value/26030/2 "2022-08-07T10:47:05Z")

</div>

If you use Grafana (9+), you can sort it there.

Use the Transform panel that is next to the Query tab and look for the **Sort by** function:

 ![20220807_124515](https://us1.discourse-cdn.com/flex023/uploads/influxdata/original/2X/1/15cae56f2984e6aabe80df2138b8bcc3cc774672.png)

---

<div class="post-metadata">

**Author:** ![kevin\_Verreth](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/kevin_verreth/32/10691_2.png) [@kevin\_Verreth](https://community.influxdata.com/u/kevin_Verreth)\
**Post date:** [August 17, 2022, 10:21am UTC](https://community.influxdata.com/t/sort-by-highest-value/26030/3 "2022-08-17T10:21:06Z")

</div>

Hi

Will this not just sort by time? I want to sort so i can see the highest value first. If i try your solution:

 ![afbeelding](https://us1.discourse-cdn.com/flex023/uploads/influxdata/original/2X/1/1209e90051a6acea9ae7fdba2f7f11702c576ab5.png)

I would presume i need to do the sorting in the flux query itself?

In Data explorer from influxDB the follwing query works, but i cannot paste this in grafana, it doenst show any value.

```auto
from(bucket: "iobroker")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "loxone.0.1990ece5-000b-a827-ffffb2ef6a95b230.value" or r["_measurement"] == "loxone.0.167f5741-037a-8b3d-ffffb2ef6a95b230.value" or r["_measurement"] == "loxone.0.1826dbac-0057-2552-ffffb2ef6a95b230.value")
  |> filter(fn: (r) => r["_field"] == "value")
  |> sort(columns: ["_time"], desc: true)
  |> last()
  |> group(columns: ["_result"], mode:"by")
  |> sort(columns: ["_value"], desc: true)

```

Kind regards  
Kevin

---

<div class="post-metadata">

**Author:** ![LordOfTheSnow](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/lordofthesnow/32/10705_2.png) [@LordOfTheSnow](https://community.influxdata.com/u/LordOfTheSnow)\
**Post date:** [August 19, 2022, 6:40pm UTC](https://community.influxdata.com/t/sort-by-highest-value/26030/4 "2022-08-19T18:40:23Z")

</div>

> Will this not just sort by time? I want to sort so i can see the highest value first.

Well then select a different field from the dropdown list.
