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

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse suscipit sapien ac sapien malesuada fringilla. Fusce venenatis, mauris id sagittis dapibus, mauris velit sollicitudin ante, a pulvinar leo orci vel erat. Nam mattis erat augue, at luctus ex dignissim et. Donec suscipit, dui at efficitur tristique, nulla nisi ornare lorem, vel dictum lectus eros nec felis.

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.
  1. Right-click the desired field in the attribute table.
  2. Select Sort Ascending, Sort Descending, or Custom Sort. An ordered field is created.

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.
  1. In the Calculate Field window, click Helper Type, and select Function.
  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.
  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.
  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.