AnsweredAssumed Answered

Arcpy SearchCursor: list fields in ascending/descending order?

Question asked by jpilbeam on Jan 31, 2018
Latest reply on Jan 31, 2018 by rvburton

Using arcpy.da.SearchCursor, how do you sort data in either ascending or descending order?

 

I noticed the old arcpy.SearchCursor had sort_fields.

arcpy.SearchCursor(dataset, {where_clause}, {spatial_reference}, {fields}, {sort_fields})

 

I have a script that uses arcpy.da.SearchCursor to create a list of table fields. The "D" in the function of line 8 is meant to put it in descending order, but it does not.

env.workspace = r'\\gisfile\GISstaff\Jared\ModelBuilder\TEST folder\TESTGDB.gdb'

#variables
intable = "ElectionResults_Nov2016"
tbList = [] #create list

#use cursor to get all unique values in field, and list them in ascending order
for row in arcpy.da.SearchCursor(intable, "ContestTitle", "D"):
    if row[0] not in tbList:
        tbList.append(row[0])

 

EDIT: I've also been looking into sorted. It's a matter of placing it in the cursor, though.

sorted()

Outcomes