1

I'm wondering if there's a way to sort my records on the fly using a searchCursor. I know you can't sort using the data access (da) module but can with the legacy function.

Without adding a record for the x,y of the polygon centroid as an attribute does anyone have any ideas?

I could grab the centroid point using SHAPE@CENTROID and then the resulting x,y with @X and @Y. I don't think I can run those all together though, but if I did it would look something like:

arcpy.SearchCursor(fc, sort_fields="SHAPE@CENTROID@X a; SHAPE@CENTROID@Y a") 
6
  • you can sort with the da cursors Commented Dec 20, 2016 at 21:00
  • 3
    pro.arcgis.com/en/pro-app/arcpy/data-access/… check out the examples on the bottom of the page Commented Dec 20, 2016 at 21:01
  • I had not seen this before... can you post this as a solution so I can mark it as solved? Commented Dec 20, 2016 at 21:09
  • Alternatively,albeit probably less efficient, is to run your data through the SORT tool, then step through that with a standard search cursor. Commented Dec 20, 2016 at 21:10
  • 1
    The Sort tool is actually what you want with a large dataset -- "merge" sorts never run short of heap and fail for lack of gigabytes of free RAM. Commented Dec 21, 2016 at 0:04

1 Answer 1

1

As commented by @ziggy this seems to be possible using the sample code at the bottom of the arcpy.da.SearchCursor() documentation.

Use SearchCursor and the Python sorted method to sort rows.For additional sorting options, see the Python Sorting Mini-HOW TO.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.