QGIS Field Calculator – Assigning Row Numbers for Sorted Tables

field-calculatorqgissorting

In QGIS 3 the Field calculator has a great function called @row_number (in QGIS 2 it was $rownum) which returns (assigns) the number of the current row.

Is there a way to make it work together with sorting?

Let's say I want to sort table by column "length" and get the order (sequence) of lines by length from shortest to longest.

I know I can use some office spreadsheet software easily for this to sort by column and populate new column with order (sequence). But I would prefer to do the whole job in QGIS.

As workaround there is also Changing order of features in shapefile – using MMQGIS Plugin and function modify/sort and after that use @row_number ($rownum) on the new saved file.

Best Answer

I have coded a plugin (Sort and Number) to solve your issue. It allows you to select up to 3 fields and order your attribute table according to these fields. Then, it numbers the attribute table in a new field (named "order" by default), starting from 1.

enter image description here