11

Field calculator has a great function under "Records" called "$rownum" which returns (assigns) the number of the current row.

Is there a way to make it work together with sorting? Lets 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.

Update: As workaround there is also How to change the order of features in a shapefile? - using mmqgis plugin and function modify/sort and after that use "$rownum" on the new saved file.

17

So, I've 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

  • It looks awesome. I am going to test this plugin now, thank you. – Miro May 3 '16 at 23:25
  • When installed for the first time in 2.8 it came up with some error but after test in 2.14 and back in 2.8 it works excellent. Thank you. – Miro May 3 '16 at 23:41
  • @Miro: you're welcome. I hope it will be useful for other people too. – ArMoraer May 4 '16 at 6:04
  • @ArMoraer - Great plugin! – Joseph May 4 '16 at 9:29
  • 1
    Can you recode this for Bonn 3 -> i loved this tool! – lowsparked Sep 12 '18 at 22:18
7

I'm not sure you can with field calculator, but you can use a virtual layer with a query like :

SELECT 
    * ,
    (SELECT count(*) FROM your_layer AS b WHERE a.length >= b.length) AS length_position
FROM your_layer AS a 
ORDER BY a.length
  • Thank you, I still work with long term release 2.8 so it was my first time I discovered virtual layer in 2.14. Apart it was very very very slow it works. Generally this is acceptable though I am going to wait a bit more for some other answers if any. – Miro May 3 '16 at 7:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.