<?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: Sorting Records in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/sorting-records/m-p/289805#M22454</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi John,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;If your data is in a Geodatabase you can use &lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="http://resources.arcgis.com/en/help/main/10.1/index.html#//018w00000011000000" rel="nofollow" target="_blank"&gt;arcpy.da.SearchCursor&lt;/A&gt;&lt;SPAN&gt;, which allows you to specify &lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="http://www.w3schools.com/sql/sql_orderby.asp" rel="nofollow" target="_blank"&gt;ORDER BY clause of an SQL Select Statement&lt;/A&gt;&lt;SPAN&gt; as part of the sql_clause parameter.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;For example, here is a function to get an object ID (or FID for Shapefiles) of the feature with the highest value of an attribute:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;import arcpy&amp;nbsp; def get_id_of_higest(fc, attr): &amp;nbsp;&amp;nbsp;&amp;nbsp; """Return object ID of a feature with the highest value of attribute attr. &amp;nbsp;&amp;nbsp;&amp;nbsp; Returns None if fc is an empty feature class. &amp;nbsp;&amp;nbsp;&amp;nbsp; fc -- feature class (MUST BE STORED IN A GEODATABASE!!!) &amp;nbsp;&amp;nbsp;&amp;nbsp; attr -- name of the attribute to examine &amp;nbsp;&amp;nbsp;&amp;nbsp; """ &amp;nbsp;&amp;nbsp;&amp;nbsp; ret = None &amp;nbsp;&amp;nbsp;&amp;nbsp; with arcpy.da.SearchCursor(fc, ['OID@', attr], sql_clause=(None, 'ORDER BY "' + str(attr) + '" DESC')) as sc: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in sc: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ret = row[0] &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; break &amp;nbsp;&amp;nbsp;&amp;nbsp; return ret&amp;nbsp; myfc = r'c:\temp\cities.shp' myattr = 'pop_max' maxido = get_id_of_higest(myfc, myattr) # now you can use maxido to select the feature&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You might need to refactor this code to better suit your need and save computational time but hopefully it will help you to get it done.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Filip&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 10 Apr 2014 19:36:23 GMT</pubDate>
    <dc:creator>FilipKrál</dc:creator>
    <dc:date>2014-04-10T19:36:23Z</dc:date>
    <item>
      <title>Sorting Records</title>
      <link>https://community.esri.com/t5/python-questions/sorting-records/m-p/289804#M22453</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Does anyone know of a good way to sort records by attribute value without the sort_management function?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I need do the following:&lt;/SPAN&gt;&lt;BR /&gt;&lt;UL&gt;&lt;BR /&gt;&lt;LI&gt;Iterate through an FC, get the feature with the highest point value&lt;/LI&gt;&lt;BR /&gt;&lt;LI&gt;Select all of the features within 5 miles of the selected feature, &lt;/LI&gt;&lt;BR /&gt;&lt;LI&gt;Copy all of those selected features to a new FC&lt;/LI&gt;&lt;BR /&gt;&lt;LI&gt;Delete the selected from the original FC&lt;/LI&gt;&lt;BR /&gt;&lt;LI&gt;Continue iterating until the original FC has no features remaining.&lt;/LI&gt;&lt;BR /&gt;&lt;/UL&gt;&lt;SPAN&gt;I can manage everything except getting the feature with the highest attribute value in the FC. I only have a Basic license so I can't use sort management.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Any ideas?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 10 Apr 2014 19:00:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/sorting-records/m-p/289804#M22453</guid>
      <dc:creator>JohnDye</dc:creator>
      <dc:date>2014-04-10T19:00:39Z</dc:date>
    </item>
    <item>
      <title>Re: Sorting Records</title>
      <link>https://community.esri.com/t5/python-questions/sorting-records/m-p/289805#M22454</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi John,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;If your data is in a Geodatabase you can use &lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="http://resources.arcgis.com/en/help/main/10.1/index.html#//018w00000011000000" rel="nofollow" target="_blank"&gt;arcpy.da.SearchCursor&lt;/A&gt;&lt;SPAN&gt;, which allows you to specify &lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="http://www.w3schools.com/sql/sql_orderby.asp" rel="nofollow" target="_blank"&gt;ORDER BY clause of an SQL Select Statement&lt;/A&gt;&lt;SPAN&gt; as part of the sql_clause parameter.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;For example, here is a function to get an object ID (or FID for Shapefiles) of the feature with the highest value of an attribute:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;import arcpy&amp;nbsp; def get_id_of_higest(fc, attr): &amp;nbsp;&amp;nbsp;&amp;nbsp; """Return object ID of a feature with the highest value of attribute attr. &amp;nbsp;&amp;nbsp;&amp;nbsp; Returns None if fc is an empty feature class. &amp;nbsp;&amp;nbsp;&amp;nbsp; fc -- feature class (MUST BE STORED IN A GEODATABASE!!!) &amp;nbsp;&amp;nbsp;&amp;nbsp; attr -- name of the attribute to examine &amp;nbsp;&amp;nbsp;&amp;nbsp; """ &amp;nbsp;&amp;nbsp;&amp;nbsp; ret = None &amp;nbsp;&amp;nbsp;&amp;nbsp; with arcpy.da.SearchCursor(fc, ['OID@', attr], sql_clause=(None, 'ORDER BY "' + str(attr) + '" DESC')) as sc: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in sc: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ret = row[0] &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; break &amp;nbsp;&amp;nbsp;&amp;nbsp; return ret&amp;nbsp; myfc = r'c:\temp\cities.shp' myattr = 'pop_max' maxido = get_id_of_higest(myfc, myattr) # now you can use maxido to select the feature&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You might need to refactor this code to better suit your need and save computational time but hopefully it will help you to get it done.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Filip&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 10 Apr 2014 19:36:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/sorting-records/m-p/289805#M22454</guid>
      <dc:creator>FilipKrál</dc:creator>
      <dc:date>2014-04-10T19:36:23Z</dc:date>
    </item>
    <item>
      <title>Re: Sorting Records</title>
      <link>https://community.esri.com/t5/python-questions/sorting-records/m-p/289806#M22455</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;You could try using the built in Python sorting method with lists, which you can get from a Search Cursor&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Try something like the following.&amp;nbsp; Keep in mind I haven't actually tested this, but it should give you an idea.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;copiedFeatureNumber = 1

featureLayer = arcpy.mapping.Layer(featureClass)

while True:

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Break out of loop when feature class is empty
&amp;nbsp;&amp;nbsp;&amp;nbsp; if int(arcpy.GetCount_management(geodatabaseLayer).getOutput(0)) == 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; break

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Get List from Search Cursor
&amp;nbsp;&amp;nbsp;&amp;nbsp; sortedList = [row for row in arcpy.da.SearchCursor(featureLayer,["OID@","pointValue"])]
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Sort List by the Point Value
&amp;nbsp;&amp;nbsp;&amp;nbsp; sortedList = sorted(sortedList, key=lambda x: x[1], reverse=True)
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Get the FID of the largest Point Value
&amp;nbsp;&amp;nbsp;&amp;nbsp; FID = sortedList[0][0]

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Select that Feature
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management(featureLayer,"NEW_SELECTION","FID = {0}".format(FID))
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Add to Selection all features within 5 Miles of that Feature
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByLocation_management(featureLayer,"WITHIN_A_DISTANCE",featureLayer,"5 Miles","ADD_TO_SELECTION")

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Make a copy of those features for whatever use desired
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CopyFeatures_management(featureLayer,"featureLayer_Num_{0}".format(copiedFeatureNumber))

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Make a list of the selected FIDs
&amp;nbsp;&amp;nbsp;&amp;nbsp; FIDList = [row[0] for row in arcpy.da.SearchCursor(featureLayer,["OID@"])]
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Clear Selection
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management("address_final_lm","CLEAR_SELECTION")
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Iterate through that Feature Class and remove all features in the selected FID List 
&amp;nbsp;&amp;nbsp;&amp;nbsp; with arcpy.da.UpdateCursor(featureLayer, ["OID@"]) as updatecursor:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in updatecursor:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if row[0] in FIDList:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; updatecursor.deleteRow()
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; copiedFeatureNumber += 1&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This probably isn't the most efficient method, but it should get the job done.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Hope this helps!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Matt&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 13:59:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/sorting-records/m-p/289806#M22455</guid>
      <dc:creator>MattEiben</dc:creator>
      <dc:date>2021-12-11T13:59:02Z</dc:date>
    </item>
  </channel>
</rss>

