Sort (Data Management)

Summary

Reorders records in a feature class or table, in ascending or descending order, based on one or multiple fields. The reordered result is written to a new dataset.

Learn more about how Sort works

Illustration

Sort tool illustration
Change the order of a feature class or table by sorting on one or multiple fields.

Usage

  • Feature classes can be spatially reordered, or sorted. The Shape field must be used as the sort field for spatial sorting. There are a number of spatial sort methods that arrange the features differently based on their location.

  • If input records are selected, only the subset of selected records are sorted and written to the output.

  • If more than one field is set as a sort field, rows are first sorted by the first field and within that order, sorted by the second field, and so on.

  • Polygon features can be sorted by their area using the Shape_Area field of a geodatabase feature class. Similarly, polyline features can be sorted by their length using the Shape_Length field. To sort polygon features in a shapefile, add a new field, calculate the area into the new field using Calculate Field, and run Sort using the new field.

  • To transfer the input dataset's subtypes, domains, and other advanced geodatabase field properties to the output dataset, use the Transfer Geodatabase Attribute Properties environment.

  • To copy the input dataset's geodatabase attachments to the output dataset, use the Maintain Attachments environment.

Parameters

LabelExplanationData Type
Input Dataset

The input dataset with the records that will be reordered based on the field values in the sort field or fields.

Table View
Output Dataset

The output feature class or table.

Feature Class; Table
Field(s)

The field or fields whose values will be used to reorder the input records and the direction the records will be sorted.

  • Ascending—Records will be sorted from low value to high value.
  • Descending—Records will be sorted from high value to low value.
Value Table
Spatial Sort Method
(Optional)

Specifies how features will be spatially sorted. The sort method is only enabled when the Shape field is designated as one of the sort fields.

  • Upper rightSorting will start at the upper right corner. This is the default.
  • Upper leftSorting will start at the upper left corner.
  • Lower rightSorting will start at the lower right corner.
  • Lower leftSorting will start at the lower left corner.
  • Peano curveA space filling curve algorithm, also known as a Peano curve, will be used to sort.
String

arcpy.management.Sort(in_dataset, out_dataset, sort_field, {spatial_sort_method})
NameExplanationData Type
in_dataset

The input dataset with the records that will be reordered based on the field values in the sort field or fields.

Table View
out_dataset

The output feature class or table.

Feature Class; Table
sort_field
[[sort_field, direction],...]

The field or fields whose values will be used to reorder the input records and the direction the records will be sorted.

  • Ascending—Records will be sorted from low value to high value.
  • Descending—Records will be sorted from high value to low value.
Value Table
spatial_sort_method
(Optional)

Specifies how features will be spatially sorted. The sort method is only enabled when the Shape field is designated as one of the sort fields.

  • URSorting will start at the upper right corner. This is the default.
  • ULSorting will start at the upper left corner.
  • LRSorting will start at the lower right corner.
  • LLSorting will start at the lower left corner.
  • PEANOA space filling curve algorithm, also known as a Peano curve, will be used to sort.
String

Code sample

Sort example 1 (Python window)

The following Python window script demonstrates how to use the Sort function to order features by the values of a field.

import arcpy
from arcpy import env

env.workspace = "C:/data/city.gdb"

arcpy.Sort_management("crime", "crime_Sort", [["DATE_REP", "ASCENDING"]])
Sort example 2 (stand-alone script)

The following Python script demonstrates how to use the Sort function in a stand-alone script.

# Name: Sort_example2.py
# Description: Sorts wells by location and well yield.

# Import system modules
import arcpy

# Set workspace environment
arcpy.env.workspace = "C:/data/newfoundland.gdb"

# set local variables
in_dataset = "wells"
out_dataset = "wells_Sort"

# Order features first by location (Shape) and then by WELL_YIELD
sort_fields = [["Shape", "ASCENDING"], ["WELL_YIELD", "DESCENDING"]]

# Use Peano algorithm
sort_method = "PEANO"

# execute the function
arcpy.Sort_management(in_dataset, out_dataset, sort_fields, sort_method)

Related topics