9

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.

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Nov 9, 2011 at 18:50
0

2 Answers 2

12

Try using Sort (Data Management) followed by Calculate Field (Data Management) using the auto-increment example on the Calculate Field examples help page.

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.

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
answered Nov 9, 2011 at 19:14
4
  • Excellent suggestion. And if you need to do this on a recurring basis, consider linking these tools together in ModelBuilder. Commented Nov 9, 2011 at 19:39
  • 1
    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. Commented Jan 19, 2012 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. Commented Jan 19, 2012 at 4:00
  • Rather than using the global variable based increment example, you could use itertools.count generator. Commented Aug 19, 2016 at 1:45
5

I used ModelBuilder 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 ModelBuilder and it failed.

Expression:

autoIncrement()

Expression Type: PYTHON_9.3

Code Block:

rec=0
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 
 else: 
 rec = rec + pInterval 
 return rec
PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
answered May 19, 2014 at 16:23
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.