When I run the script below, I get features as ordered by
layer = iface.activeLayer() # getFeatures() ids = [f.id() for f in layer.getFeatures()] print(ids) # OUTPUT: [0L, 1L, ..... 499L, 500L]
If I select all features and run the next script, I get the same output.
# selectedFeatures() ids = [f.id() for f in layer.selectedFeatures()] print(ids) # OUTPUT: [0L, 1L, ..... 499L, 500L]
But if I select manually some features -for example features with
12, 13, 17id in my shapefile- and run the previous script, I get unordered list of
id. I always get the same output, no matter what the selection order is.
ids = [f.id() for f in layer.selectedFeatures()] print(ids) # OUTPUT: [17L, 12L, 13L] ## OR selectedFeaturesIterator() ids = [f.id() for f in layer.selectedFeaturesIterator()] print(ids) # OUTPUT: [17L, 12L, 13L]
selectedFeaturesIterator() return unordered features by
getFeatures() returns ordered features? What is the logic behind?
EDIT: The point I wonder is not whether features are ordered in database/file/memory or not. What I mean by "why" is that why a method returns ordered features but another one returns unordered or same method returns different results. Pay attention to
selectedFeatures(). As an example, if all features are selected, it returns [1, 2, 3, 4]. But it returns [2, 1, 3] if some features are selected.
selectedFeaturesIterator()return an iterator (
selectedFeatures()returns a list (
QGIS 3 has
selectedFeaturesIterator()in QGIS 2.
I get the same result in both QGIS v2.18 and v3.2.