[QGIS-Developer] QgsFeatureRequest SortByOrder

Matthias Kuhn matthias at opengis.ch
Mon Jun 4 00:14:13 PDT 2018


Hi Jorge,

There are two possibilities, one is to use the full-blown API with
OrderBy class etc. And then there is a shorthand for this, called
QgsFeatureRequest.addOrderBy which is enough in 90% of the cases.

Short version:

  request = QgsFeatureRequest()
  request.addOrderBy('field_name')

https://qgis.org/pyqgis/master/core/Feature/QgsFeatureRequest.html#qgis.core.QgsFeatureRequest.addOrderBy

Long version:

  clause = QgsFeatureRequest.OrderByClause('field_name')
  orderBy = QgsFeatureRequest.OrderBy([clause])
  request = QgsFeatureRequest()
  request.setOrderBy(orderBy)

https://qgis.org/pyqgis/master/core/Feature/QgsFeatureRequest.html#qgis.core.QgsFeatureRequest.setOrderBy

Regards Matthias

On 06/01/2018 06:15 PM, Jorge Almerio wrote:
> Can anyone help me how to iterate through features using a Field as a
> sort order?
> 
>  
> 
> I saw that there is a function setOrderBy
> <https://qgis.org/api/classQgsFeatureRequest.html#ad9c11d4c88185fcfb0a54f8703699717>on
> QgsFeatureRequest, by it seems to request a OrderBy Class argument, but
> I could not get how to use it.
> 
>  
> 
> Simple example:
> 
> "
> 
> lyr = iface.activeLayer()
> 
> prov = lyr.dataProvider()
> 
> for feature in prov.getFeatures(QgsFeatureRequest()):
> 
>     print feature.id()
> 
> "
> 
>  
> 
> Thank you
> 
> 
> 
> _______________________________________________
> QGIS-Developer mailing list
> QGIS-Developer at lists.osgeo.org
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
> 


More information about the QGIS-Developer mailing list