6

If I open a shapefile in two empty QGIS projects and view the attribute table the rows are in completely different orders.

How does QGIS decide which order to display the rows in an attribute table and is there a way of making a shapefile display its attribute table in consistent row order? I'm using QGIS 3.4.

Just checked in version 2.14 - row order is consistent so it seems to be a version issue?

3
  • 1
    does it matter? if it is important pick a column and sort on that one.
    – Ian Turton
    Commented Jul 11, 2019 at 11:01
  • It might be useful to play around with $id and @row_number in the field calculator and see what numbers you get
    – miln40
    Commented Jul 11, 2019 at 11:02
  • 1
    It's not the end of the world, but it's a downgrade on version 2.14 where no extra work was required to sort a table to make it consistent and you could guarantee that when another user opened the data they'd see the same rows at the top as you did
    – ian
    Commented Jul 11, 2019 at 12:08

2 Answers 2

6

This is a known bug in QGIS 3.2. https://issues.qgis.org/issues/19299, answered in this post Attribute Table Default Order in QGIS

In QGIS 3.x the attribute rows are shown in attribute table by the order of which each one was created. Try to open your shapefile and put the mouse pointer on the first line like in the image below and QGIS will show you the creation order of that feature. Or you can sort the features by an field like id.QGIS 3.x feature creation order

1
  • Thanks @Francisco Camello it seems the bug is still present in 3.4
    – ian
    Commented Jul 11, 2019 at 12:03
6

As a complement to @Francisc0Camello answer. If you want to change the order of the features inside the shapefile permanently, you can use the "Order by Expression" tool.

enter image description here

You can use a single column, or you can create an expression that combines several columns to order by several columns. For example, the following example would sort features by state, and then by county name.

"STATE" + "NAME"
3
  • This didn't work for me though it should, but the help also points out that this is inconsistent Commented Mar 4, 2021 at 2:01
  • You may want to check the result by opening the .dbf in libre-office (or excel) to check the order. In my case the order was as specified in the sort expression, i.e. OK. The order in which qgis displays after the sort may still follow earlier specs. BTW never change the order of the dbf outside qgis (or whatever gis you use).
    – Jan
    Commented Mar 22, 2021 at 8:49
  • I found that using a gpkg, exporting the Order by expression output to tab, then saving the tab as a gpkg (with fid unticked) worked for me
    – Buff Fox
    Commented Jun 14, 2023 at 23:31

Your Answer

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

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