Using arcpy.da.SearchCursor, how do you sort data in either ascending or descending order?
I noticed the old arcpy.SearchCursor had sort_fields.
arcpy<SPAN class="punctuation token">.</SPAN>SearchCursor<SPAN class="punctuation token">(</SPAN>dataset<SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">{</SPAN>where_clause<SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">{</SPAN>spatial_reference<SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">{</SPAN>fields<SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">{</SPAN>sort_fields<SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN></SPAN>I have a script that uses arcpy.da.SearchCursor to create a list of table fields. The "D" in the function of line 8 is meant to put it in descending order, but it does not.
env<SPAN class="punctuation token">.</SPAN>workspace <SPAN class="operator token">=</SPAN> r<SPAN class="string token">'\\gisfile\GISstaff\Jared\ModelBuilder\TEST folder\TESTGDB.gdb'</SPAN>
<SPAN class="comment token">#variables</SPAN>
intable <SPAN class="operator token">=</SPAN> <SPAN class="string token">"ElectionResults_Nov2016"</SPAN>
tbList <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="comment token">#create list </SPAN>
<SPAN class="comment token">#use cursor to get all unique values in field, and list them in ascending order</SPAN>
<SPAN class="keyword token">for</SPAN> row <SPAN class="keyword token">in</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>SearchCursor<SPAN class="punctuation token">(</SPAN>intable<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"ContestTitle"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"D"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">if</SPAN> row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">not</SPAN> <SPAN class="keyword token">in</SPAN> tbList<SPAN class="punctuation token">:</SPAN>
tbList<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
EDIT: I've also been looking into sorted. It's a matter of placing it in the cursor, though.
sorted<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN></SPAN>