Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am in ArcMap and I want to sort the records based on a field and then do a Calculate Field with an auto-incrementing value with the sort in-place. Any ideas?

It looks like I could get an update cursor on the feature class then do an auto-increment, but I would still like to see if there is a way to do this in the Field Calculator in ArcMap.

share|improve this question
up vote 1 down vote accepted

FYI...I used model builder and I did a field sort followed by calculate field using the sort code below and it worked great. Previously, I had tried these two steps outside of model builder and it failed.



Expression Type: PYTHON_9.3

Code Block:

def autoIncrement():
    global rec
    pStart = 1 #adjust start value, if req'd 
    pInterval = 1 #adjust interval value, if req'd
    if (rec == 0): 
        rec = pStart 
        rec = rec + pInterval 
    return rec
share|improve this answer
This doesnt really answer what the OP asked for – snowgage Apr 22 at 20:12

Try using Sort (Data Management) followed by Calculate Field (Data Management) using the auto-increment example here.

If you need to sort the data and update it in-place (no intermediate dataset), then I think you would have to use an UpdateCursor which can also sort by a field.

share|improve this answer
Excellent suggestion. And if you need to do this on a recurring basis, consider linking these tools together in ModelBuilder. – RyanDalton Nov 9 '11 at 19:39
The ESRI sample for sequential numbering is based on the FID (original row ID). I want the numbering to be based on the current sort -can this be done? In the code from ESRI what is "global" rec? Thanks. – GeorgeC Jan 19 '12 at 3:41
My answer hopefully already answers your first question in two ways: 1) use Sort (Data Management), or 2) use an UpdateCursor with the sort fields argument. I do not think you can access the current table view's sort state through arcpy, though it may be possible through ArcObjects. As for your second question, global makes the variable persist between calculations (one calculation per row) so that it can store the running count used in each calculation. – blah238 Jan 19 '12 at 4:00

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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