I am trying to create an new unique id field in a feature class. I already have one field called "SITE_ID_FD", but it is historical. The format of the unique value in that field isn't what our current format is, so I am creating a new field with the new format.

Old Format = M001, M002, K003, K004, S005, M006, etc

New format = 12001, 12002, 12003, 12004, 12005, 12006, etc

I wrote the following script:

fc = r"Z:\test.gdb\testfc"

x = 12001

cursor = arcpy.UpdateCursor(fc)

for row in cursor:
    row.setValue("SITE_ID", x)
    x+= 1

This works fine, but it populates the new id field based on the default sorting of objectID. I need to sort 2 fields first and then populate the new id field based on that sorting (I want to sort by a field called 'SITE' and then by the old id field "SITE_ID_FD")

I tried running the script from the python window in ArcMap after manually sorting the 2 fields in hopes that the python would honor the sort, but it doesn't. I'm not sure how to do this in python. Can anyone suggest a method?


Try using the sort_fields parameter of the UpdateCursor function, e.g.:

cursor = arcpy.UpdateCursor(fc, , , , "SITE A; SITE_ID_FD A")

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.