As far as I know, You can only use the function sort() to sort metrics by value.

  • According to this PR, Prometheus does not intend to provide the function sort_by_label().
  • According to this Issue, Grafana displays the query results from Prometheus without sorting.
  • According to this Issue, Grafana supports sorting by value when displaying legend.

In Grafana 7, Prometheus metrics can be transformed from time series format to table format using the Transform module, so that you can sort the metrics by any label or value.


In December 2023, prometheus v2.49 finally added sort_by_label() and sort_by_label_desc()

Answer from LeoHsiao on Stack Overflow
🌐
Prometheus
prometheus.io › docs › prometheus › latest › querying › functions
Query functions | Prometheus
sort(v instant-vector) returns vector elements sorted by their float sample values, in ascending order. Histogram samples in the input vector are ignored silently. Please note that sort only affects the results of instant queries, as range query results always have a fixed output ordering.
Discussions

promql: sort_by_label should fallback to a consistent ordering by metric, not by sample value
Proposal I got an idea while thinking about #12503 : Currently, if sort_by_label encounters the same label values to sort by, it falls back to sorting by sample value. However, by using sort_by_lab... More on github.com
🌐 github.com
6
June 11, 2024
Prometheus sort by help (screenshot to explain)
Create a transform to arrange the columns and sort how you want. More on reddit.com
🌐 r/grafana
2
1
December 4, 2020
prometheus - Sort in promql such that all metrics with one lebel appear first in result - Stack Overflow
It uses an additional function - sort - for sorting time series by values, before sorting time series by metric names. ... Sign up to request clarification or add additional context in comments. ... Save this answer. ... Show activity on this post. You can use the operator "or" to join the PromQLs, ... More on stackoverflow.com
🌐 stackoverflow.com
Prometheus Grafana Templating order by count - Stack Overflow
I am trying to put a dropdown for each API end point which will show the QPS and Latency of http requests (RED metrics). I used Grafana's templating and used the following prometheus query. More on stackoverflow.com
🌐 stackoverflow.com
🌐
HatchJS
hatchjs.com › home › promql sort by value: how to order time series data by their values
PromQL Sort by Value: How to Order Time Series Data by Their Values
January 5, 2024 - In this article, we will show you how to sort time series data by value using PromQL. We will cover both ascending and descending order, and we will show you how to use the `sort` operator to sort by multiple fields.
🌐
Promlabs
training.promlabs.com › training › understanding-promql › advanced-querying › sorting-and-topk-bottomk
Understanding PromQL | Prometheus Trainings by PromLabs
In this section, you will learn ... to sort the output series by their sample value. You can achieve this by using the sort() (ascending sort) and sort_desc() (descending sort) functions....
🌐
GitHub
github.com › prometheus › prometheus › issues › 14289
promql: sort_by_label should fallback to a consistent ordering by metric, not by sample value · Issue #14289 · prometheus/prometheus
June 11, 2024 - Proposal I got an idea while thinking about #12503 : Currently, if sort_by_label encounters the same label values to sort by, it falls back to sorting by sample value. However, by using sort_by_lab...
Author: prometheus
🌐
Grafana
grafana.com › blog › inside-promql-a-closer-look-at-the-mechanics-of-a-prometheus-query
Inside PromQL: A closer look at the mechanics of a Prometheus query | Grafana Labs
October 9, 2024 - These compute a result from the ... and not on samples, and are handled specially. Sorting functions that take an instant vector and sort by either values or labels....
🌐
Google Groups
groups.google.com › g › prometheus-developers › c › 5T1BDDGynUg
topk() sorting
January 15, 2021 - I'm pretty sure (grafana 2.6) you can click the header of the table (small blue text) and it will sort by that column.
🌐
Reddit
reddit.com › r/grafana › prometheus sort by help (screenshot to explain)
r/grafana on Reddit: Prometheus sort by help (screenshot to explain)
December 4, 2020 -

Hello,

I have this table in Grafana and the data is perfect but I can get it to sort by instance name, if fact I'm not sure what method it's using to sort. If I enable prometheus at the bottom in blue all the data is correct but the instance order messes up, if I turn it off they are all in order.

The first column is the instance with server01 to server15, I need it to sort in numerical order, is this possible?

Find elsewhere
🌐
Google Groups
groups.google.com › g › prometheus-users › c › xCRsAXQ_tPg
PromQL query to sort by numbers
I have a metric that I'd like to sort by numbers. Currently it is sorted like this: 0, 1, 11, 12, 13... 2, 20,21... etc. ... Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message ... How do you mean you have a metric sorted like that? Do you mean that you have a metric with the given values ...
🌐
GitHub
github.com › prometheus › prometheus › pull › 1533
Added sort_by_label function for sorting by label values. by pstibrany · Pull Request #1533 · prometheus/prometheus
FYI, I put this wider topic on the dev summit agenda under "Extending PromQL". I still think this feature has merit and I think the time is ripe to revisit older discussions within the project. Side note: Adding +1 comments does not help the discussion at large, it's largely noise; this is literally why GitHub implemented reactions to comments. ... Disclaimer: Not a shill. MetricsQL from VictoriaMetrics supports sorting by label value.
Author: prometheus
🌐
SigNoz
signoz.io › guides › how to sort legends by series name in prometheus and grafana
How to Sort Legends by Series Name in Prometheus and Grafana | SigNoz
September 24, 2024 - However, sorting large datasets with many series can potentially impact rendering time. Yes. You can use PromQL functions like sort() and sort_desc() to control the order of data before it reaches Grafana in case of instant queries.
🌐
Grafana
community.grafana.com › configuration
Extracting variables from Prometheus labels, sorting by value - Configuration - Grafana Labs Community Forums
September 4, 2018 - I’ve got a couple of related questions regarding variables and sorting. I’d like to display a fairly large set of single-stat panels, using repeat. These are essentially red/green panels based on the value of a Prometheus query (green = 0, red = > 0). To improve the usability of this, I’d ...
🌐
Grafana
community.grafana.com › dashboards
Sorting series by a label value - Dashboards - Grafana Labs Community Forums
November 1, 2023 - What Grafana version and what operating system are you using? I am using v10.2.0 under Docker Desktop (Macbook Pro - M2). What are you trying to achieve? Ultimately I am trying to display the RX bitrate of a switch’s interfaces under one visualisation in the correct order (ports 1-30).
🌐
VictoriaMetrics
docs.victoriametrics.com › victoriametrics › metricsql
VictoriaMetrics: MetricsQL
Query language reference. Enhanced PromQL with additional functions, histogram helpers, and subquery support.
🌐
GitHub
github.com › grafana › grafana › issues › 10208
grafana doesn't sort results returned by prometheus correctly. · Issue #10208 · grafana/grafana
December 14, 2017 - The following query works well on the prometheus query GUI, the records are always displayed in descending order, sort_desc(sum(rate(my_call_count[1h])*3600) by (tenant)) But it doesn't work as expected when the above query is used in th...
Author: grafana
🌐
Grafana
community.grafana.com › prometheus
Grafana did not display the records in the expected order in table - Prometheus - Grafana Labs Community Forums
December 29, 2017 - I created a table on grafana, and the metrics for the table is as below, sort_desc(avg(my_test_count) by (tenantid)) I expect that the records should be displayed in decending order based on the metrics value, but actually it isn’t the case. Note that the data displayed in table is correct, but the order isn’t expected.
🌐
Google Groups
groups.google.com › g › prometheus-users › c › J39e5hGxFY4
Sort values in grafana
Once you graph, some series might have a larger value at one point in the graph, but a lower value at another point. Also, whatever the sorting returned by Prometheus is, the position of a line in a graph is only dependent on the x/y values of its points (unless you stack, then it could affect the ordering of which line is stacked ontop of another).