How to permanently reclassify "Sort" the attribute table records based on a field value ? I try the geoprocessing tool Sort (Data Management), but this tool always need to write a new output feature or table with a correct values, what I need is to permanently sort the value on the same table or feature class.
The only way to be sure that features are returned in a specific order is to specify an
ORDER BY clause in the query (assuming, of course, that the data source supports
ORDER BY queries). Note that this can have serious performance consequences, since it may force the optimizer to use a much less efficient query plan, or force an extra pass through the database's sort area after it would have already returned results. It is best practice to avoid
ORDER BY queries unless it is absolutely necessary.
Physically changing the contents of a table is the only way to give a preference for returning features in a specific order. The only safe way to do this in-situ is to use a clustered index (again, assuming that clustered indexes are possible). All other means must destroy and replace the source table. Since input destruction is potentially unreliable, there are no common solutions to implement it.
Finally, reorganizing a table on the basis of one set of columns makes it less efficient to search on all other columns. This is particularly true of large and very large tables (over 500k rows), where fragmenting the table on a attribute basis can have a significant impact on spatial searches. Since ArcGIS applies a spatial filter on nearly every query, sorting on anything but spatial order (or a functional equivalent) can significantly (and permanently) impair most spatial queries, without actually guaranteeing preferred return order.
TLDR summation: Sorting tables without a significant investment in performance review/analysis is not wise practice.
I think my solution was not proposed yet...
- Copy the feature class you want to sort into a temporary
- Sort that temporary feature class and output (overwrite) the sorted feature class into your input.
Probably not the fastest solution, but a simple and effective one
import arcpy, os in_feature_class = os.path.join("C:", "temp", "my_input_fc.shp") temp_feature_class = arcpy.CopyFeatures_management(in_feature_class, "in_memory\\temp") arcpy.Sort_management(temp_feature_class, in_feature_class, [["SORT_FIELD", "ASCENDING"]])
You can permanently sort your data with the following script. Make a backup, because this will overwrite the original data.
edit: as noted in the comments to this post, and other answers, this script does not guarantee proper order and will hamper performance on large tables. It works fine on small tables, though, so use it at your own risk.
data =  with arcpy.da.SearchCursor("YOUR_FEATURE_CLASS","*") as cursor: for row in cursor: data.append(row) data.sort(key=lambda tup: tup) # sorts based on the 4th field, change if you want. i = 0 with arcpy.da.UpdateCursor("YOUR_FEATURE_CLASS","*") as cursor: for row in cursor: row = data[i] i += 1 cursor.updateRow(row)