6

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.

Taras
35.8k5 gold badges77 silver badges152 bronze badges
asked Jun 28 at 18:17
9
  • 3
    You 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. Commented Jun 28 at 19:55
  • 1
    would permanently sorting the table a satisfying solution for you? Commented Jun 29 at 6:24
  • 2
    Is a Python script an option? Then try gis.stackexchange.com/a/422195/51035 Commented Jun 29 at 13:22
  • 1
    One more PyQGIS solution available here: gis.stackexchange.com/questions/447872/… Commented Jul 1 at 6:54
  • 1
    Just 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. Commented Jul 1 at 7:42

2 Answers 2

4

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"
)
answered Jun 29 at 15:10
4

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.

enter image description here

enter image description here

answered Jun 29 at 15:17
1
  • 3
    It 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." Commented Jun 30 at 6:45

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.