# Grafana w/ InfluxDB: How to sort based on field key?

**URL:** <https://community.grafana.com/t/grafana-w-influxdb-how-to-sort-based-on-field-key/52997>\
**Category:** InfluxDB\
**Tags:** influxdb, stat-panel\
**Created:** [September 8, 2021, 1:44am UTC](https://community.grafana.com/t/grafana-w-influxdb-how-to-sort-based-on-field-key/52997 "2021-09-08T01:44:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ycsin](https://avatars.discourse-cdn.com/v4/letter/y/3bc359/32.png) [@ycsin](https://community.grafana.com/u/ycsin)\
**Post date:** [September 8, 2021, 1:44am UTC](https://community.grafana.com/t/grafana-w-influxdb-how-to-sort-based-on-field-key/52997/1 "2021-09-08T01:44:34Z")

</div>

Grafana: v8.1.2  
InfluxDB: v2.0.8

I’m using the flux query language. I have a data structure like so:

\_time  
\_measurement  
|- Battery  
\_tag  
|- Unit ID  
\_field  
|- b1c1 (Battery bank 1, cell 1)  
|- b1c2  
|- b1c3  
…  
|- b1c10  
|- b1c11  
|- b1c12

When I do a query on “Battery”, renaming the keys by regex to just 1, 2 … 12 in Grafana, and display the fields in a “Stat” panel, Grafana will display it like so: 1, 10, 11, 12, 2, 3, 4 … instead of the numerical ascending order.

I currently workaround this by doing 12 queries in the order I want but that is quite painful, is there any option in InfluxDB / Grafana that I can use to do this?

---

<div class="post-metadata">

**Author:** ![anon70503803](https://avatars.discourse-cdn.com/v4/letter/a/8797f3/32.png) [@anon70503803](https://community.grafana.com/u/anon70503803)\
**Post date:** [September 17, 2021, 3:09am UTC](https://community.grafana.com/t/grafana-w-influxdb-how-to-sort-based-on-field-key/52997/2 "2021-09-17T03:09:27Z")

</div>

Hi @ycsin

It might be helpful for us here if you shared your panel json. Paste it into a code block using triple back-ticks (```) in your response?

---

<div class="post-metadata">

**Author:** ![ycsin](https://avatars.discourse-cdn.com/v4/letter/y/3bc359/32.png) [@ycsin](https://community.grafana.com/u/ycsin)\
**Post date:** [September 17, 2021, 3:54am UTC](https://community.grafana.com/t/grafana-w-influxdb-how-to-sort-based-on-field-key/52997/3 "2021-09-17T03:54:46Z")

</div>

Hi @anon70503803,

thanks for the reply.

```json
{
  "id": 23763571993,
  "gridPos": {
    "h": 26,
    "w": 6,
    "x": 12,
    "y": 21
  },
  "type": "stat",
  "title": "🔋 Bank 1",
  "transformations": [
    {
      "id": "renameByRegex",
      "options": {
        "regex": "b1c(\\d+) .*",
        "renamePattern": "$1"
      }
    }
  ],
  "datasource": "${DB}",
  "pluginVersion": "8.1.2",
  "maxDataPoints": 50,
  "interval": "60s",
  "fieldConfig": {
    "defaults": {
      "thresholds": {
        "mode": "absolute",
        "steps": [
          {
            "color": "red",
            "value": null
          },
          {
            "color": "light-orange",
            "value": 3.5
          },
          {
            "color": "green",
            "value": 3.8
          },
          {
            "color": "red",
            "value": 4.2
          }
        ]
      },
      "mappings": [],
      "color": {
        "mode": "thresholds"
      },
      "decimals": 2,
      "max": 4.3,
      "min": 3.3,
      "unit": "volt"
    },
    "overrides": []
  },
  "options": {
    "reduceOptions": {
      "values": false,
      "calcs": [],
      "fields": ""
    },
    "orientation": "horizontal",
    "text": {},
    "textMode": "name",
    "colorMode": "value",
    "graphMode": "area",
    "justifyMode": "auto"
  },
  "targets": [
    {
      "query": "from(bucket: \"BMS\")\r\n |> range(start: v.timeRangeStart, stop: v.timeRangeStop)\r\n |> filter(fn: (r) => r[\"uid\"] == \"${UID}\")\r\n |> filter(fn: (r) => r[\"_measurement\"] == \"BMS\")\r\n |> filter(fn: (r) => r[\"_field\"] =~ /b1c.*/)\r\n |> sort(columns: [\"_time\"], desc: true)\r\n |> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)\r\n |> yield(name: \"mean\")",
      "rawQuery": true,
      "refId": "1"
    }
  ],
  "description": "",
  "timeFrom": null,
  "timeShift": null
}

```

Currently it looks like this:

 ![image](https://us1.discourse-cdn.com/grafana/original/3X/1/3/1303249ff2afd4de461dc615e09a91c0950acd1f.png)

I’d like them to be in this order instead:

 ![image](https://us1.discourse-cdn.com/grafana/original/3X/6/e/6ee81161855ece0047273f1a8b58c263da9a0cdd.png)

InfluxDB:

 ![image](https://us1.discourse-cdn.com/grafana/original/3X/7/4/74212c44ecf704b32676d54130968c7af5c0821e.png)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/grafana/original/2X/e/ef5308cc58c9e5a13187b5012eb99f406928462f.png) [@system](https://community.grafana.com/u/system)\
**Post date:** [September 8, 2022, 1:45am UTC](https://community.grafana.com/t/grafana-w-influxdb-how-to-sort-based-on-field-key/52997/4 "2022-09-08T01:45:21Z")

</div>

This topic was automatically closed after 365 days. New replies are no longer allowed.
