8

I have an ArcPy Data Access update cursor that I would like to sort before making updates to it.

The cursor makes updates if I don't sort, but if I add a sort to the cursor I get an error, "iteration not started."

The sort is working correctly but it must be busting the cursor for some reason.

Is it not possible to sort an update cursor like a search cursor? Does it change the type or something?

    idList = ['100100', '100200', '100300', '200100']
    count = 0
    with arcpy.da.UpdateCursor(newFC, ("SHAPE@X", "SHAPE@Y", "Label")) as addLabelCursor:            
        for row in sorted(addLabelCursor, key=itemgetter(0), reverse = True): 
            print idList[count]
            row[2] = str(idList[count])
            addLabelCursor.updateRow(row)
            count += 1
3
  • If I watch the locals in a debug session the "addLabelCursor" remains a da.UpdateCursor object all the way through. Its not like its getting converted to a list when its sorted.
    – TurboGus
    Aug 9, 2012 at 16:07
  • It is failing at: addLabelCursor.updateRow(row)
    – TurboGus
    Aug 9, 2012 at 16:08
  • You need to sort within the cursor call using an sql clause (ORDER BY)
    – jbalk
    Dec 21, 2016 at 5:04

3 Answers 3

11

The sort completely exhausts the cursor before you go into updateRow. The way that cursors work is that the updateRow call doesn't work on arbitrary rows out-of-seqence, but on the current one being iterated over. That is, you can only operate on one row at a time.

You may want to look into doing this on the geodatabase side instead. You can specify an ORDER BY clause and sort it before you get to it instead.

2
  • It looks like you cannot use the geometry tokens in the sql clause of a cursor. I'll have to use a work around like adding it to a real field before hand or maybe I will just store this cursors input in memory and use a search cursor to sort and insert cursor to create. Thanks for confirming that I cannot sort an update cursor and still update.
    – TurboGus
    Aug 9, 2012 at 17:43
  • 7
    @TurboGus but you can sort the rows in a cursor. The help indicates you can specify the sql_clause (tuple) parameter: sql_clause=(None, 'ORDER BY SORT_FIELD ASC') -- or DESC for descending.
    – nmpeterson
    Oct 17, 2014 at 20:31
2

As @nmpeterson pointed out, you can input an sql clause into the cursor call to sort by a field:

UpdateCursor (in_table, field_names, {where_clause}, {spatial_reference}, {explode_to_points}, {sql_clause})

SQL prefix supports None, DISTINCT, and TOP. SQL postfix supports None, ORDER BY, and GROUP BY.

sql_clause=(None, 'ORDER BY SORT_FIELD ASC') -- or DESC

info comes from the ESRI help: http://resources.arcgis.com/en/help/main/10.2/index.html#//018w00000014000000

0

It may be only relevant for future reference, but according to ArcGis online help when using arcpy.UpdateCursor instead of arcpy.da.UpdateCursor there is an sort option available.

cursor = arcpy.UpdateCursor(fc,sort_fields="YOURFIELDNAME")

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.