HOW TO

Sort and create a sequentially ordered ID field in an attribute table in ArcGIS Pro

Last Published: January 29, 2024

Summary

Sorting and creating a sequentially ordered ID field in an attribute table in ArcGIS Pro is an efficient way to organize data. The largest and smallest values, or perhaps the most and least significant features in a layer become easily recognizable.

Procedure

The following steps describe how to sort and create a sequentially ordered ID field in an attribute table in ArcGIS Pro using three different options: the sort ascending or sort descending function, the calculate field function, or a Python script.

Using the Sort Ascending or Sort Descending function

  1. In the Contents pane, right-click the desired feature layer, and click Attribute Table.
The Contents pane's menu with the Attribute Table option.
  1. Right-click the desired field in the attribute table.
  2. Select Sort Ascending, Sort Descending, or Custom Sort. An ordered field is created.
The table view pane and the three sorting options.

Using the Calculate Field function

Note:
This option only generates sequential numbers for unsorted data based on the OBJECTID order. If the data is sorted, the generated numbers are not sequential.
  1. Open the attribute table of the desired feature.
  2. Right-click the desired field in the attribute table.
  3. Select Calculate Field to open the Calculate Field window.
The table view pane's menu and the Calculate Field option.
  1. In the Calculate Field window, click Helper Type, and select Function.
The Calculate Field pane and the Helper Type menu.
  1. Double-click the Sequential Number function. This generates the Expression and the Code Block section to create the sequential numbers, as shown in the next image. The full example code is shown at the end of the next section.
The Calculate Field's Expression section configured with the SequentialNumber() function.
  1. Click OK. The sequential numbers based on the OBJECTID field are created in the field specified by the Calculate Field function.

Using a Python script through the Python window

The following steps demonstrate how to create sorted sequential numbers using the autoIncrement() ArcPy function:

  1. Start the Python console. Click the Analysis tab, click the down arrow next to Python, and select Python Window.
The Analysis tab and the Python drop-down menu.
  1. Import the ArcPy library.
import arcpy
  1. Set the desired feature, the field to base the order, and the field to populate the sequential numbers.
sortFeat  = r'[Geodatabase]\[Feature]'
sortField = '[Base Field to sort]'
idField   = '[Field to populate sequential numbers]'
Note:
To create the sequential numbers in a new field, create a new field and specify the new field name in the idField segment. Refer to ArcGIS Pro: Add Field for more information on creating a new field.
  1. Define a counter parameter.
rec = 0
  1. Start the autoIncrement() function.
def autoIncrement():
    global rec
    pStart    = 1
    pInterval = 1
    if (rec == 0):
        rec = pStart
    else:
        rec += pInterval
    return rec
  1. Populate the field with the sequential numbers, and delete any extra rows.
rows = arcpy.UpdateCursor(sortFeat, "", "", "", sortField + " A")

for row in rows:
    row.setValue(idField, autoIncrement())
    rows.updateRow(row)

del row, rows

This is the full script used in this article.

import arcpy

sortFeat  = r'C:\Users\\Northridge.gdb\Stations' 
sortField = 'OBJECTID' 
idField   = 'Station' 
rec=0

def autoIncrement():
    global rec
    pStart    = 1 
    pInterval = 1 
    if (rec == 0): 
        rec = pStart 
    else: 
        rec += pInterval 
    return rec

rows = arcpy.UpdateCursor(sortFeat, "", "", "", sortField + " A")

for row in rows:
    row.setValue(idField, autoIncrement())
    rows.updateRow(row)

del row, rows
  1. Place the cursor at the end of the expression and press Enter twice on the keyboard to run the script.

Article ID: 000018847

Software:
  • ArcGIS Pro 3 0
  • ArcGIS Pro 2 8 x
  • ArcGIS Pro 2 x

Receive notifications and find solutions for new or common issues

Get summarized answers and video solutions from our new AI chatbot.

Download the Esri Support App

Related Information

Discover more on this topic

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options