Is it possible to sort multiple fields at the same time in QGIS?
For example, I'd like to sort a table by state name, and then within each state, sort by county name.
This would be the same as the Advanced Sort option available in ArcMap.
There is actually a very simple trick. Suppose you want to sort by field "A" first, and then by field "B" within "A". First left click on the header of the field "B" (click once for ascending sort, a second time for descending) in the attribute table. Then left click on the header of field "A" (once for ASC, 2nd time for DESC). This last action will have sorted field "A", while preserving the sorting of field "B" within identical values of "A". Tried it with 3 fields and it also works (then got bored). I'm using QGIS 3.6.3-Noosa.
You can use the Sort and Number plugin. It allows you to sort an attribute table by up to 3 fields, then it creates an additional field that contains the order of the sorted features.
Disclaimer: I'm the author of the plugin.
If is OK to save the result permanently you can sort the shapefile with GDAL
ogr2ogr -f "ESRI Shapefile" -dialect sqlite -sql "SELECT * FROM my_shapefile ORDER BY attr_1, attr_2, attr_3" sorted.shp my_shapefile.shp
Took less that 3 minutes with a shapefile having one million polygons.
The DB Manager plugin in QGIS now has a 'Virtual Layers' node that allows you to write SQL commands against shapefiles loaded into your QGIS project.
So now that you have this ability, the SQL logic to sort by multiple fields is in the ORDER BY statement, using asc (ascending), desc (Descending), and you can string them together to sort by multiple columns:
An added bonus is you'll learn more SQL so when you get sick of shapefiles and most to PostGIS the leap won't be so great!!!
This can now be done without any plugins or database-specific work, at least for version 3.2+. To sort by multiple columns:
concat()along with the fields you want to sort by, in the proper order (ex.
concat("column1", "column2")). An output preview should be shown below your code.
Well, it is 2018 now so I can't believe this is not answered yet.
Open the attribute table view and right-click on any column, then choose Sort...
Dialog window Configure attribute table sort order is open now with full expression editor as well
Use COALESCE function with the desired columns, for example, this will sort a table by 3 columns (first by ID then VERSION and then fid)
Clicking OK will sort the table
You can't (at least not at the moment). Sorry!
See the QGIS documentation and scroll down to the very last line.
A nasty work-around would be to pull the Shapefile's DBF into Excel. Sort it there and save the result. Alternatively import your Shapefile into PostGIS or SpatialLite where you have greater sorting functionality. Basically, you will have to by-pass QGIS for advanced sorting.
As suggested by @Sylvester Sneekly, if your data is loaded into an RDBMS like PosGIS or SpatialLite, you could consider creating a database view in the RDBMS that presorts the tabular data, and then load the presorted view into QGIS as a layer.