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")
  • you can sort with the da cursors – ziggy Dec 20 '16 at 21:00
  • 3
    pro.arcgis.com/en/pro-app/arcpy/data-access/… check out the examples on the bottom of the page – ziggy Dec 20 '16 at 21:01
  • I had not seen this before... can you post this as a solution so I can mark it as solved? – ShaunLangley Dec 20 '16 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. – Hornbydd Dec 20 '16 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. – Vince Dec 21 '16 at 0:04
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.