Sort (Data Management)

License Level:BasicStandardAdvanced

Summary

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

Learn more about how Sort works

Illustration

Sort by three attributes

Usage

Syntax

Sort_management (in_dataset, out_dataset, sort_field, {spatial_sort_method})
ParameterExplanationData Type
in_dataset

The input dataset whose records will be reordered based on the field values in the sort field(s).

Table View
out_dataset

The output feature class or table.

Feature Class;Table
sort_field
[[Sort Field, Direction],...]

Specifies the field(s) whose values will be used to reorder the input records, and the direction the records will be sorted.

  • ASCENDINGRecords are sorted from low value to high value.
  • DESCENDINGRecords are sorted from high value to low value.
Value Table
spatial_sort_method
(Optional)

Specifies how features are spatially sorted. Sort method is only enabled when 'Shape' is selected as one of the sort fields.

  • URSorting starts at upper right corner. This is the default.
  • ULSorting starts at upper left corner.
  • LRSorting starts at lower right corner.
  • LLSorting starts at lower left corner.
  • PEANOSorting uses a space filling curve algorithm, also known as a peano curve.
String

Code Sample

Sort example 1 (Python window)

The following Python window script demonstrates how to use Sort 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 Python script)

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

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

# Import system modules
import arcpy
from arcpy import env

try:
    # Set workspace environment
    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)
    
    print arcpy.GetMessages()

except arcpy.ExecuteError:
    # Print error messages
    print arcpy.GetMessages(2)
    
except Exception as ex:
    print ex.args[0]

Environments

Related Topics

Licensing Information

ArcGIS for Desktop Basic: Limited
ArcGIS for Desktop Standard: Limited
ArcGIS for Desktop Advanced: Yes
11/18/2013