<?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: Custom Sort Fields and Iterate through to Assign Sequential Sort IDs in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/custom-sort-fields-and-iterate-through-to-assign/m-p/1376709#M69764</link>
    <description>&lt;P&gt;I agree about the parentheses.&amp;nbsp; As my original post indicated, the code was untested.&amp;nbsp; &amp;nbsp;The post was intended to show the structure of how I typically tackle this kind of problem, but I must not have had time to design a real world test case to do an actual debug of the code at that time.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Anyway, I hope you have gotten what you needed.&lt;/P&gt;</description>
    <pubDate>Wed, 31 Jan 2024 19:17:55 GMT</pubDate>
    <dc:creator>RichardFairhurst</dc:creator>
    <dc:date>2024-01-31T19:17:55Z</dc:date>
    <item>
      <title>Custom Sort Fields and Iterate through to Assign Sequential Sort IDs</title>
      <link>https://community.esri.com/t5/python-questions/custom-sort-fields-and-iterate-through-to-assign/m-p/1376632#M69758</link>
      <description>&lt;P&gt;Python version: 3.9.18&lt;/P&gt;&lt;P&gt;I am trying to identify drainage structures that failed two inspections in a row.&amp;nbsp; To do so, I am making a copy of the related inspections table, then sort it by "NewGUID" (id field that ties inspections to parent drainage structures" and "date_" descending.&amp;nbsp; I have tested this workflow manually, and am now looking to automate it, but I'm having trouble.&amp;nbsp; Sorting by two fields is only available with the Advanced license, which I do not have.&lt;/P&gt;&lt;P&gt;After doing some research, I found what appears to be a similar situation here:&lt;/P&gt;&lt;P&gt;&lt;A href="https://community.esri.com/t5/python-questions/increment-field-in-sorted-table/td-p/514221" target="_blank"&gt;https://community.esri.com/t5/python-questions/increment-field-in-sorted-table/td-p/514221&lt;/A&gt;&lt;/P&gt;&lt;P&gt;I took the draft code provided by &lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/3788"&gt;@RichardFairhurst&lt;/a&gt; and adapted it to my features and fields, and it now looks like this:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy

sourceFC = r"Z:\Projects\StormwaterInspectionsSelection\StormwaterInspectionsSelection.gdb\InspectionsCustomSorted20240130" 
sourceFieldsList = ['NewGUID', 'date_', 'OBJECTID']

# Use list comprehension to build a dictionary from a da SearchCursor 
valueDict = {tuple(r[0:]):0 for r in arcpy.da.SearchCursor(sourceFC, sourceFieldsList, sql_clause=(None, "ORDER BY NEWGuid, date_ DESC"))}

counter = 0
for item in sorted(valueDict.keys):
    counter += 1
    valueDict[item] = counter

sourceFieldsList.append("SortID")

with arcpy.da.UpdateCursor(sourceFC, sourceFieldsList, sql_clause=(None, "ORDER BY NEWGuid, date_ DESC")) as updateRows: 
    for updateRow in updateRows: 
        keyValue = tuple(updateRow[0:-1]) 
        # verify that the keyValue is in the Dictionary 
        if keyValue in valueDict:
            updateRow[-1] = valueDict[keyValue]
            updateRows.updateRow(updateRow)
del valueDict&lt;/LI-CODE&gt;&lt;P&gt;When I run that, I receive this error message:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
In  [38]:
Line 10:    for item in sorted(valueDict.keys):

TypeError: 'builtin_function_or_method' object is not iterable
---------------------------------------------------------------------------&lt;/LI-CODE&gt;&lt;P&gt;I searched online for that error and it seems that &lt;EM&gt;sorted&lt;/EM&gt; is a built-in function and apparently can't be iterated.&amp;nbsp; Is there a way around this?&lt;/P&gt;&lt;P&gt;Please let me know if there's any more information needed.&amp;nbsp; Any help would be greatly appreciated!&lt;/P&gt;</description>
      <pubDate>Wed, 31 Jan 2024 17:58:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/custom-sort-fields-and-iterate-through-to-assign/m-p/1376632#M69758</guid>
      <dc:creator>TownofSandwichGISTechnician</dc:creator>
      <dc:date>2024-01-31T17:58:56Z</dc:date>
    </item>
    <item>
      <title>Re: Custom Sort Fields and Iterate through to Assign Sequential Sort IDs</title>
      <link>https://community.esri.com/t5/python-questions/custom-sort-fields-and-iterate-through-to-assign/m-p/1376655#M69759</link>
      <description>&lt;P&gt;As I recall, the sorted function may fail if any value it is trying to sort is None, since the sorted function must compare all values to each other to determine the order of the values, and None cannot be compared to other actual values the way that underlying function is written.&amp;nbsp; Since one of the&amp;nbsp;fields making up your tuple key is a date field I imagine it can contain None values.&amp;nbsp; In any case you could print the keys prior to the sorted function to determine what the actual key values are to see if this may explain the code behavior.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You can try using a value substitution in your list comprehension as shown in the top reply to this post: &lt;A href="https://stackoverflow.com/questions/30976124/sort-when-values-are-none-or-empty-strings-python" target="_blank" rel="noopener"&gt;https://stackoverflow.com/questions/30976124/sort-when-values-are-none-or-empty-strings-python&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 31 Jan 2024 18:36:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/custom-sort-fields-and-iterate-through-to-assign/m-p/1376655#M69759</guid>
      <dc:creator>RichardFairhurst</dc:creator>
      <dc:date>2024-01-31T18:36:31Z</dc:date>
    </item>
    <item>
      <title>Re: Custom Sort Fields and Iterate through to Assign Sequential Sort IDs</title>
      <link>https://community.esri.com/t5/python-questions/custom-sort-fields-and-iterate-through-to-assign/m-p/1376665#M69760</link>
      <description>&lt;P&gt;keys()&lt;/P&gt;&lt;LI-CODE lang="python"&gt;z = {3: 0, 2: 5, 1:9}

for i in sorted(z.keys()):
    print(i)
    
1
2
3&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;perhaps&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 31 Jan 2024 18:36:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/custom-sort-fields-and-iterate-through-to-assign/m-p/1376665#M69760</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2024-01-31T18:36:04Z</dc:date>
    </item>
    <item>
      <title>Re: Custom Sort Fields and Iterate through to Assign Sequential Sort IDs</title>
      <link>https://community.esri.com/t5/python-questions/custom-sort-fields-and-iterate-through-to-assign/m-p/1376680#M69761</link>
      <description>&lt;P&gt;I tested this and confirmed&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/3788"&gt;@RichardFairhurst&lt;/a&gt;&amp;nbsp;is correct, but I get a different error&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;TypeError: '&amp;lt;' not supported between instances of 'NoneType' and 'str'&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;I think it's a syntax issue. There should be open and closing parenthesis after keys.&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;for item in sorted(valueDict.keys()):&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 31 Jan 2024 18:42:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/custom-sort-fields-and-iterate-through-to-assign/m-p/1376680#M69761</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2024-01-31T18:42:37Z</dc:date>
    </item>
    <item>
      <title>Re: Custom Sort Fields and Iterate through to Assign Sequential Sort IDs</title>
      <link>https://community.esri.com/t5/python-questions/custom-sort-fields-and-iterate-through-to-assign/m-p/1376684#M69762</link>
      <description>&lt;P&gt;like in my example&lt;/P&gt;</description>
      <pubDate>Wed, 31 Jan 2024 18:48:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/custom-sort-fields-and-iterate-through-to-assign/m-p/1376684#M69762</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2024-01-31T18:48:01Z</dc:date>
    </item>
    <item>
      <title>Re: Custom Sort Fields and Iterate through to Assign Sequential Sort IDs</title>
      <link>https://community.esri.com/t5/python-questions/custom-sort-fields-and-iterate-through-to-assign/m-p/1376709#M69764</link>
      <description>&lt;P&gt;I agree about the parentheses.&amp;nbsp; As my original post indicated, the code was untested.&amp;nbsp; &amp;nbsp;The post was intended to show the structure of how I typically tackle this kind of problem, but I must not have had time to design a real world test case to do an actual debug of the code at that time.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Anyway, I hope you have gotten what you needed.&lt;/P&gt;</description>
      <pubDate>Wed, 31 Jan 2024 19:17:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/custom-sort-fields-and-iterate-through-to-assign/m-p/1376709#M69764</guid>
      <dc:creator>RichardFairhurst</dc:creator>
      <dc:date>2024-01-31T19:17:55Z</dc:date>
    </item>
    <item>
      <title>Re: Custom Sort Fields and Iterate through to Assign Sequential Sort IDs</title>
      <link>https://community.esri.com/t5/python-questions/custom-sort-fields-and-iterate-through-to-assign/m-p/1376711#M69765</link>
      <description>&lt;P&gt;Thank you, &lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/3788"&gt;@RichardFairhurst&lt;/a&gt;! There were a few records that didn't have a date_ value, and one that didn't have a NewGUID.&amp;nbsp; I resolved those, and added the parenthesis, as suggested&amp;nbsp; by &lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/215600"&gt;@DanPatterson&lt;/a&gt; and &lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/191789"&gt;@BlakeTerhune&lt;/a&gt;.&amp;nbsp; The script ran nearly instantly.&amp;nbsp; Thank you all for your help!&lt;/P&gt;&lt;P&gt;Josh&lt;/P&gt;</description>
      <pubDate>Wed, 31 Jan 2024 19:26:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/custom-sort-fields-and-iterate-through-to-assign/m-p/1376711#M69765</guid>
      <dc:creator>TownofSandwichGISTechnician</dc:creator>
      <dc:date>2024-01-31T19:26:58Z</dc:date>
    </item>
  </channel>
</rss>

