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.
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It only takes a minute to sign up.
Sign up to join this communityThis 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.This functionality was added via issue #1137, and covered in the latest (development) version of the user manual.
concat("col1", right( ('000' || tostring( "col2" )), 4))
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.
sort()
function, I wonder why it is so slow. I'll try to find some trick to make the process faster in a future version.
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!!!
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.
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)
coalesce("ID","VERSION","fid" )
Clicking OK will sort the table
PyQgis solution:
# set order by field (twice)
clause1 = qgis.core.QgsFeatureRequest.OrderByClause(field1, ascending=True)
clause2 = qgis.core.QgsFeatureRequest.OrderByClause(field2, ascending=True)
orderby = qgis.core.QgsFeatureRequest.OrderBy([clause1, clause2])
request.setOrderBy(orderby)
layer = qgis.core.QgsVectorLayer('layer_path', 'layer_name', "provider") # add layer
features = layer.getFeatures(request)
for feature in features:
attrs = feature.attributes()
print(attrs)
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.
I know this is old, but I keep ending up here from a search. Adding on to other answers, and using QGIS 3.28 Firenze.
Right-click "sort..." on a column to get the expression editor.
Sort multiple string columns (from Eron Lloyd's answer):
concat("col1", "col2", "col3")
Sort mixed string and numeric columns, use lpad for the numeric columns and multiply to remove the decimal if needed:
concat("str_col", lpad("int_col", 10, 0), lpad(round("decimal_col",2),10,0))
You may need to adjust the lpad second argument larger if you’ve got a bigger number range.
For real numbers, the decimal throws the sorting off. Rounding to a consistent number of decimals resolves this. I haven’t seen this documented, which is why I’m posting.
Using lpad is the workaround for mixed data types, suggested here in the 3.28 manual.