At QGIS 3.40.8, I am working with large tables (1+ million records) that often need to be sorted in some descending order.
It is simple to sort an attribute table in ascending order by left-clicking once on the desired field name. A second click will then sort the table in descending order. Unfortunately, this takes twice as long as a direct descending sort.
A descending sort can also be accomplished by right-clicking and selecting the Sort... option, then working with the resulting expression box. But this is a rather kludge way to solve a simple task.
Neither of the methods above is very efficient at quickly creating a descending sort, particularly with large tables. I'm looking for some sort of 1-click method to sort a table in descending order. An internet search did not turn up anything useful.
Question: How to directly sort a table in descending order?
The tables are either in GPKG or FGDB format. Additionally, they may or may not contain geometries.
-
3You don't say what format your data is in which will effect sorting. I'd go with a postgis table with an index on the attribute, sorted in your preferred order.Ian Turton– Ian Turton2025年06月28日 19:55:45 +00:00Commented Jun 28 at 19:55
-
1would permanently sorting the table a satisfying solution for you?sn1ks– sn1ks2025年06月29日 06:24:47 +00:00Commented Jun 29 at 6:24
-
2Is a Python script an option? Then try gis.stackexchange.com/a/422195/51035bugmenot123– bugmenot1232025年06月29日 13:22:08 +00:00Commented Jun 29 at 13:22
-
1One more PyQGIS solution available here: gis.stackexchange.com/questions/447872/…Taras– Taras ♦2025年07月01日 06:54:55 +00:00Commented Jul 1 at 6:54
-
1Just work with Views/Virtual Tables if you need a one-click solution. I assume you're not just sorting for funsies, so maybe try to create contextual summaries, e.g. 'top or bottom 10 by attribute', 'distinct attribute' or whatever you need to vizualize or relate.geozelot– geozelot2025年07月01日 07:42:30 +00:00Commented Jul 1 at 7:42
2 Answers 2
Use Field calculator to create a new field sort
that contains the descending sort order. To do so, use this expresssion and replace fieldname
with the name of the field that you want to use for sorting:
array_find (
array_sort (
array_agg("fieldname"),
0
),
"fieldname"
)
Right click the column header (containing the fieldname) and select Sort...
. In the dialog window that opens, insert the fieldname you want to use for sorting and uncheck the box Sort ascending
.
-
3It looks like this method has been already suboptimal by the OP: "A descending sort can also be accomplished by right-clicking and selecting the Sort... option, then working with the resulting expression box. But this is a rather kludgey way to solve a simple task."Andrea Giudiceandrea– Andrea Giudiceandrea2025年06月30日 06:45:40 +00:00Commented Jun 30 at 6:45
Explore related questions
See similar questions with these tags.