In the following code I meant to select rows from a table of cities named fc where the COUNTY='Socorro County' and ORDERED BY NAME:
fc="cities_copy"
where_selection=""""COUNTY"='Socorro County'"""
sql=(None,"ORDER BY NAME")
cursor=arcpy.da.SearchCursor(fc,["NAME","COUNTY"],where_selection,sql_clause=sql)
for row in cursor:
print("City Name: {0},\t {1}".format(row[0],row[1]))
From this code I get a list of cities by Name and County of those cities only in Socorro County, the original table selected from has cites with many other counties. I wish to order the selected cites by the NAME field, which I specified in my SQL variable. Instead I get this which is not ordered by NAME:
City Name: Veguita, Socorro County
City Name: Sabinal, Socorro County
City Name: Abeytas, Socorro County
City Name: Las Nutrias, Socorro County
City Name: Bernardo, Socorro County
Which is not ordered by NAME field. What am I doing wrong?
The recommended way to sort columns from Arcpy is this:
import arcpy
fc = 'c:/data/base.gdb/well'
fields = ['WELL_ID', 'WELL_TYPE']
# Use ORDER BY sql clause to sort field values
for row in arcpy.da.SearchCursor(
fc, fields, sql_clause=(None, 'ORDER BY WELL_ID, WELL_TYPE')):
print(u'{0}, {1}'.format(row[0], row[1]))
After reading: https://community.esri.com/t5/python-questions/sql-clause-in-arcpy-da-searchcursor-is-not/td-p/51603
I found this reference in the documentation of ArcGIS: https://pro.arcgis.com/en/pro-app/latest/help/analysis/geoprocessing/basics/the-in-memory-workspace.htm
Limitations Memory-based workspaces have the following limitations:
Memory-based workspaces do not support geodatabase elements such as feature datasets, relationship classes, attribute rules, contingent values, field groups, spatial or attribute indexes, representations, topologies, geometric networks, or network datasets.
Specifically Attribute indexes means you cant sort.