<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Sort feature class field permanently in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/sort-feature-class-field-permanently/m-p/1290332#M67672</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/102454"&gt;@2Quiker&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;&lt;P&gt;Blake, this is helpful but it doesn't physically change the order of the SUB_NAME field.&lt;/P&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;I can't think of a practical reason why you would need to reorder the records in the table. Every method for viewing the data allows for sorting it as needed. If you wanted to change the order of the records in the table (beyond an ad hoc sort), you need to recreate the table. May I ask what your requirement is that you need to reorder the rows in the table?&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/102454"&gt;@2Quiker&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;&lt;P&gt;Would it best to place this shapefile in to a file geodatabase?&lt;/P&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;Yes. When working with Esri products, I highly recommend using a file geodatabase over a shapefile. You can always export it back to a shapefile later if you need to share it that way.&lt;/P&gt;</description>
    <pubDate>Wed, 17 May 2023 19:46:41 GMT</pubDate>
    <dc:creator>BlakeTerhune</dc:creator>
    <dc:date>2023-05-17T19:46:41Z</dc:date>
    <item>
      <title>Sort feature class field permanently</title>
      <link>https://community.esri.com/t5/python-questions/sort-feature-class-field-permanently/m-p/1289851#M67655</link>
      <description>&lt;P&gt;I need to permanently sort a feature class field so I can add a sequential number based on the sort. I know I can use the sort tool, but I want to sort the existing feature without having to create another feature class. I am able to do the sequential number with the following but I need the feature class "Sub_Name" sorted. anyone have an example of how to accomplish this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy
from itertools import count
cntr = count()

fc = 'C:\Temp\SUBS.shp'
with arcpy.da.UpdateCursor(fc,["ID","SUB_NAME"],sql_clause=(None,"ORDER BY SUB_NAME")) as cur:
    for i, j in cur:
        cur.updateRow([next(cntr), j])&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 16 May 2023 22:13:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/sort-feature-class-field-permanently/m-p/1289851#M67655</guid>
      <dc:creator>2Quiker</dc:creator>
      <dc:date>2023-05-16T22:13:07Z</dc:date>
    </item>
    <item>
      <title>Re: Sort feature class field permanently</title>
      <link>https://community.esri.com/t5/python-questions/sort-feature-class-field-permanently/m-p/1289859#M67656</link>
      <description>&lt;P&gt;Sort tool then truncate and append the data back into the original.&amp;nbsp; No easy way to do in-place as far as I know.&amp;nbsp; Maybe an update cursor but it's the same principle but more convoluted.&lt;/P&gt;</description>
      <pubDate>Tue, 16 May 2023 22:24:16 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/sort-feature-class-field-permanently/m-p/1289859#M67656</guid>
      <dc:creator>DavidPike</dc:creator>
      <dc:date>2023-05-16T22:24:16Z</dc:date>
    </item>
    <item>
      <title>Re: Sort feature class field permanently</title>
      <link>https://community.esri.com/t5/python-questions/sort-feature-class-field-permanently/m-p/1289864#M67658</link>
      <description>&lt;P&gt;You should read the &lt;A href="https://pro.arcgis.com/en/pro-app/latest/arcpy/data-access/updatecursor-class.htm" target="_self"&gt;documentation&lt;/A&gt;.&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;&lt;SPAN class=""&gt;DISTINCT&lt;/SPAN&gt;,&amp;nbsp;&lt;SPAN class=""&gt;ORDER BY&lt;/SPAN&gt;, and&amp;nbsp;&lt;SPAN class=""&gt;ALL&amp;nbsp;&lt;/SPAN&gt;are only supported when working with databases. They are not supported by other data sources (such as dBASE or INFO tables).&lt;/P&gt;&lt;P&gt;&lt;SPAN class=""&gt;TOP&amp;nbsp;&lt;/SPAN&gt;is only supported by&lt;SPAN class=""&gt;SQL Server&lt;/SPAN&gt;databases.&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;For a shapefile, you will need to read all the data into a list, and then enumerate the sorted list. Here's an untested code sample.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy

subs_shp = 'C:\Temp\SUBS.shp'

sub_name_values = [row[0] for row in arcpy.da.SearchCursor(subs_shp,["SUB_NAME"])]

sub_name_id = {}
for enum, sub_name in enumerate(sorted(sub_name_values)):
    sub_name_id[sub_name] = enum

with arcpy.da.UpdateCursor(subs_shp,["ID","SUB_NAME"]) as u_cursor:
    for id, sub_name in u_cursor:
        id = sub_name_id[sub_name]
        u_cursor.updateRow([id, sub_name])&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 16 May 2023 22:45:16 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/sort-feature-class-field-permanently/m-p/1289864#M67658</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2023-05-16T22:45:16Z</dc:date>
    </item>
    <item>
      <title>Re: Sort feature class field permanently</title>
      <link>https://community.esri.com/t5/python-questions/sort-feature-class-field-permanently/m-p/1290177#M67669</link>
      <description>&lt;P&gt;Blake, this is helpful but it doesn't physically change the order of the SUB_NAME field.&lt;/P&gt;&lt;P&gt;I have the following and it does sort the&amp;nbsp;SUB_NAME field of the&amp;nbsp; shapefile but somehow it spatially moves the features, not sure why.&lt;/P&gt;&lt;P&gt;Would it best to place this shapefile in to a file geodatabase?&lt;/P&gt;&lt;LI-CODE lang="python"&gt;fc = 'C:\Temp\SUBS.shp'

datatosort = [] 
with arcpy.da.SearchCursor(fc,"*") as cursor: 
    for row in cursor: 
       datatosort.append(row) 

subindex = 2
rowindex = 0 
datatosort.sort(key=lambda tup: tup[subindex]) 

with arcpy.da.UpdateCursor(fc,"*") as cursor: 
    for row in cursor: 
       row = datatosort[rowindex] 
       rowindex += 1 
       cursor.updateRow(row)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 17 May 2023 16:47:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/sort-feature-class-field-permanently/m-p/1290177#M67669</guid>
      <dc:creator>2Quiker</dc:creator>
      <dc:date>2023-05-17T16:47:05Z</dc:date>
    </item>
    <item>
      <title>Re: Sort feature class field permanently</title>
      <link>https://community.esri.com/t5/python-questions/sort-feature-class-field-permanently/m-p/1290332#M67672</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/102454"&gt;@2Quiker&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;&lt;P&gt;Blake, this is helpful but it doesn't physically change the order of the SUB_NAME field.&lt;/P&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;I can't think of a practical reason why you would need to reorder the records in the table. Every method for viewing the data allows for sorting it as needed. If you wanted to change the order of the records in the table (beyond an ad hoc sort), you need to recreate the table. May I ask what your requirement is that you need to reorder the rows in the table?&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/102454"&gt;@2Quiker&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;&lt;P&gt;Would it best to place this shapefile in to a file geodatabase?&lt;/P&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;Yes. When working with Esri products, I highly recommend using a file geodatabase over a shapefile. You can always export it back to a shapefile later if you need to share it that way.&lt;/P&gt;</description>
      <pubDate>Wed, 17 May 2023 19:46:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/sort-feature-class-field-permanently/m-p/1290332#M67672</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2023-05-17T19:46:41Z</dc:date>
    </item>
    <item>
      <title>Re: Sort feature class field permanently</title>
      <link>https://community.esri.com/t5/python-questions/sort-feature-class-field-permanently/m-p/1291058#M67683</link>
      <description>&lt;P&gt;That .dbf of that shapefile is using in a different reporting system. I was trying to save having to manually sort every time the .dbf is used. I run this reporting once a day or sometimes more often.&lt;/P&gt;</description>
      <pubDate>Fri, 19 May 2023 14:26:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/sort-feature-class-field-permanently/m-p/1291058#M67683</guid>
      <dc:creator>2Quiker</dc:creator>
      <dc:date>2023-05-19T14:26:59Z</dc:date>
    </item>
    <item>
      <title>Re: Sort feature class field permanently</title>
      <link>https://community.esri.com/t5/python-questions/sort-feature-class-field-permanently/m-p/1291175#M67686</link>
      <description>&lt;P&gt;When you create the shapefile, you'll have to do it row by row, no export or update. Do you need a shapefile (with geometry) or can it be just a table of values (CSV)?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 19 May 2023 17:19:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/sort-feature-class-field-permanently/m-p/1291175#M67686</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2023-05-19T17:19:25Z</dc:date>
    </item>
    <item>
      <title>Re: Sort feature class field permanently</title>
      <link>https://community.esri.com/t5/python-questions/sort-feature-class-field-permanently/m-p/1291186#M67687</link>
      <description>&lt;P&gt;I need a shapfile with geometry.&lt;/P&gt;</description>
      <pubDate>Fri, 19 May 2023 17:42:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/sort-feature-class-field-permanently/m-p/1291186#M67687</guid>
      <dc:creator>2Quiker</dc:creator>
      <dc:date>2023-05-19T17:42:52Z</dc:date>
    </item>
    <item>
      <title>Re: Sort feature class field permanently</title>
      <link>https://community.esri.com/t5/python-questions/sort-feature-class-field-permanently/m-p/1291204#M67688</link>
      <description>&lt;P&gt;Are you creating it from a shapefile or a geodatabase feature class?&lt;/P&gt;</description>
      <pubDate>Fri, 19 May 2023 18:08:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/sort-feature-class-field-permanently/m-p/1291204#M67688</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2023-05-19T18:08:39Z</dc:date>
    </item>
    <item>
      <title>Re: Sort feature class field permanently</title>
      <link>https://community.esri.com/t5/python-questions/sort-feature-class-field-permanently/m-p/1291221#M67689</link>
      <description>&lt;P&gt;I will take your advice and create it in a geodatabase.&lt;/P&gt;</description>
      <pubDate>Fri, 19 May 2023 18:41:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/sort-feature-class-field-permanently/m-p/1291221#M67689</guid>
      <dc:creator>2Quiker</dc:creator>
      <dc:date>2023-05-19T18:41:04Z</dc:date>
    </item>
    <item>
      <title>Re: Sort feature class field permanently</title>
      <link>https://community.esri.com/t5/python-questions/sort-feature-class-field-permanently/m-p/1291243#M67690</link>
      <description>&lt;P&gt;No, I'm asking about the source data. How do you create the&amp;nbsp;SUBS.shp?&lt;/P&gt;</description>
      <pubDate>Fri, 19 May 2023 19:27:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/sort-feature-class-field-permanently/m-p/1291243#M67690</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2023-05-19T19:27:39Z</dc:date>
    </item>
    <item>
      <title>Re: Sort feature class field permanently</title>
      <link>https://community.esri.com/t5/python-questions/sort-feature-class-field-permanently/m-p/1291271#M67694</link>
      <description>&lt;P&gt;Sorry, it derived from a feature class in a geodatabase.&lt;/P&gt;&lt;P&gt;I preform a selection by location, then I export the selected features to the shapefile.&lt;/P&gt;</description>
      <pubDate>Fri, 19 May 2023 21:21:27 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/sort-feature-class-field-permanently/m-p/1291271#M67694</guid>
      <dc:creator>2Quiker</dc:creator>
      <dc:date>2023-05-19T21:21:27Z</dc:date>
    </item>
    <item>
      <title>Re: Sort feature class field permanently</title>
      <link>https://community.esri.com/t5/python-questions/sort-feature-class-field-permanently/m-p/1291287#M67695</link>
      <description>&lt;P&gt;Maybe try something like this then.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy
import os

source_fc = r"C:\Temp\some_gdb\SUBS"
sort_field_name = "SUB_NAME"

# Make new feature class for ordered rows.
out_fc = arcpy.management.CreateFeatureclass(
    out_path=os.path.dirname(source_fc),
    out_name="SUBS_ordered",
    template=source_fc
)

# Insert the rows ordered by sub_name
source_fc_fields = [f.name for f in arcpy.ListFields(source_fc)]
with arcpy.da.SearchCursor(source_fc, "*", sql_clause=(None, f"ORDER BY {sort_field_name}")) as s_cursor:
    with arcpy.da.InsertCursor(out_fc, "*") as i_cursor:
        for row in s_cursor:
            i_cursor.insertRow(row)&lt;/LI-CODE&gt;</description>
      <pubDate>Fri, 19 May 2023 21:27:57 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/sort-feature-class-field-permanently/m-p/1291287#M67695</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2023-05-19T21:27:57Z</dc:date>
    </item>
    <item>
      <title>Re: Sort feature class field permanently</title>
      <link>https://community.esri.com/t5/python-questions/sort-feature-class-field-permanently/m-p/1293189#M67730</link>
      <description>&lt;P&gt;I tried to incorporate you suggestion, but I ran into an issue with the Global ID. At least I believe that is the issue.&lt;/P&gt;&lt;P&gt;TypeError: sequence size must match size of the row.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;import arcpy, os

project = arcpy.mp.ArcGISProject("CURRENT")

arcpy.env.workspace = os.path.dirname(project.filePath)
wp = os.path.dirname(project.filePath)
outPath = r'C:\Temp\Scratchworkspace.gdb'

scr = 'SUBS'
sortedTableName = 'SUBS_ordered'
arcpy.management.MakeFeatureLayer("SUBS", "memory\SubsLyr")
arcpy.SelectLayerByLocation_management("SUBS", "WITHIN_A_DISTANCE", "SUBJECT_PROPERTY", "1 Mile", "NEW_SELECTION")

arcpy.analysis.Select("SUBS", r"memory/SubLayer1")

#--check if output feature class exists, if not, create it
if not os.path.exists(os.path.join(outPath,sortedTableName)):
    out_fc = arcpy.management.CreateFeatureclass(outPath,out_name="SUBS_ordered", template=scr)
else:
    try:
        arcpy.DeleteRows_management(os.path.join(outPath,"SUBS_ordered"))
    except:
        pass
    
source_fc = r"memory/SubLayer1"
sort_field_name = "SUB_NAME"
source_fc_fields = [f.name for f in arcpy.ListFields(scr)]
with arcpy.da.SearchCursor(source_fc, "*", sql_clause=(None, f"ORDER BY {sort_field_name}")) as s_cursor:
    with arcpy.da.InsertCursor(sortedTableName, "*") as i_cursor:
        for row in s_cursor:
            i_cursor.insertRow(row)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The "SUBS" is in SDE geodatabase and has GlobalID .&lt;/P&gt;&lt;P&gt;The "SUBS_ordered" in a file geodatabase doesn't have a GlobalID.&lt;/P&gt;&lt;P&gt;How can I leave out the GlobalID or other fields?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The other difference between the two is.&lt;/P&gt;&lt;P&gt;SubLayer1: Shape_STArea__ , Shape_STLength__&lt;/P&gt;&lt;P&gt;SUBS_ordered : Shape_Area, Shape_Length&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 25 May 2023 18:08:35 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/sort-feature-class-field-permanently/m-p/1293189#M67730</guid>
      <dc:creator>2Quiker</dc:creator>
      <dc:date>2023-05-25T18:08:35Z</dc:date>
    </item>
    <item>
      <title>Re: Sort feature class field permanently</title>
      <link>https://community.esri.com/t5/python-questions/sort-feature-class-field-permanently/m-p/1293216#M67731</link>
      <description>&lt;P&gt;They should have the same fields if you're creating the output using the subs as a template. It was hard to follow your code because there seemed to be some unused lines, redundancies, and too many different names. I did my best to refactor it. If there's a GlobalID field in Subs, it should create a GlobalID field in the Subs_Ordered output too.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy
import os

project = arcpy.mp.ArcGISProject("CURRENT")

arcpy.env.workspace = os.path.dirname(project.filePath)
arcpy.env.overwriteOutput = True
outPath = r'C:\Temp\Scratchworkspace.gdb'

source_fc = "SUBS"
sortedTableName = "SUBS_ordered"

# Select subs by location to subject property.
arcpy.management.MakeFeatureLayer(source_fc, "SubsLyr")
arcpy.management.SelectLayerByLocation(
    in_layer="SubsLyr",
    overlap_type="WITHIN_A_DISTANCE",
    select_features="SUBJECT_PROPERTY",
    search_distance="1 Mile",
    selection_type="NEW_SELECTION"
)

# Make new feature class for ordered rows.
out_fc = arcpy.management.CreateFeatureclass(
    out_path=outPath,
    out_name=sortedTableName,
    template="SubsLyr"
)

# Read through the sorted selection of subs and insert them into the output feature class.
with arcpy.da.SearchCursor("SubsLyr", "*", sql_clause=(None, "ORDER BY SUB_NAME")) as s_cursor:
    with arcpy.da.InsertCursor(sortedTableName, "*") as i_cursor:
        for row in s_cursor:
            i_cursor.insertRow(row)&lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 25 May 2023 18:46:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/sort-feature-class-field-permanently/m-p/1293216#M67731</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2023-05-25T18:46:00Z</dc:date>
    </item>
  </channel>
</rss>

