Sort (Data Management)
Summary
This tool reorders, in ascending or descending order, records in a feature class or table based on field values. The reordered result is copied to a new dataset.
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.
-
Geodatabase and SDE feature classes, shapefiles, feature layers, tables, and table views are valid inputs. Individual components of CAD and SDC datasets are also valid inputs.
-
If any input records are selected, only the subset of selected records are sorted and copied 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. If you want 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.
For the Field(s) parameter, sorting by the Shape field or by multiple fields is only available with an ArcInfo license. Sorting by any single attribute field (excluding Shape) is available at all license levels.
Syntax
Parameter | Explanation | Data 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.
| 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.
| String |
Code Sample
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/input/sort.gdb" arcpy.Sort_management("crime", "crime_Sort", [["DATE_OCCURRED", "ASCENDING"]])
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 inDataset = "wells" outDataset = "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(inDataset, outDataset, sort_fields, sort_method) except: # Print error messages arcpy.GetMessages()