<?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 grid (text) field sort of like a number! in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/sort-grid-text-field-sort-of-like-a-number/m-p/598386#M46826</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Sure, you can play around with .replace(). This will replace all " &amp;amp; " with "-", stored in variable newString:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;...
for row in cursor:
&amp;nbsp;&amp;nbsp;&amp;nbsp; newString = row[1].replace(" &amp;amp; ", "-")&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sun, 12 Dec 2021 01:41:57 GMT</pubDate>
    <dc:creator>DarrenWiens2</dc:creator>
    <dc:date>2021-12-12T01:41:57Z</dc:date>
    <item>
      <title>Sort grid (text) field sort of like a number!</title>
      <link>https://community.esri.com/t5/python-questions/sort-grid-text-field-sort-of-like-a-number/m-p/598375#M46815</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello all,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I'm not sure if python can do something like this. Our brains can do it so maybe we can get an automated way to do this?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I need to give a page number to each of these pages. They need to be in an order like below, based on the grid name. It's like township name, then some of them have a section detail page (11-1), and then some of them further have a subsection detail page (11-1NE - northeast part of section 1 in township 11). I have 275 pages in this set. In the past I have to do it manually. Does anyone have any ideas? Thank you.&lt;/P&gt;&lt;P&gt;1&lt;/P&gt;&lt;P&gt;2&lt;/P&gt;&lt;P&gt;3&lt;/P&gt;&lt;P&gt;4&lt;/P&gt;&lt;P&gt;5&lt;/P&gt;&lt;P&gt;6&lt;/P&gt;&lt;P&gt;7&lt;/P&gt;&lt;P&gt;8&lt;/P&gt;&lt;P&gt;9&lt;/P&gt;&lt;P&gt;10&lt;/P&gt;&lt;P&gt;11&lt;/P&gt;&lt;P&gt;11-1&lt;/P&gt;&lt;P&gt;11-1NE&lt;/P&gt;&lt;P&gt;11-1NW&lt;/P&gt;&lt;P&gt;11-1SE&lt;/P&gt;&lt;P&gt;11-2&lt;/P&gt;&lt;P&gt;11-2NE&lt;/P&gt;&lt;P&gt;11-7&lt;/P&gt;&lt;P&gt;11-8&lt;/P&gt;&lt;P&gt;11-10SW&lt;/P&gt;&lt;P&gt;11-12NE&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 23 Jun 2015 18:24:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/sort-grid-text-field-sort-of-like-a-number/m-p/598375#M46815</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2015-06-23T18:24:30Z</dc:date>
    </item>
    <item>
      <title>Re: Sort grid (text) field sort of like a number!</title>
      <link>https://community.esri.com/t5/python-questions/sort-grid-text-field-sort-of-like-a-number/m-p/598376#M46816</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You can use the Sort tool, in the General toolbox (data management) to sort on a field. To use it in Python, use the following syntax:&lt;/P&gt;&lt;P&gt;Sort_management (in_dataset, out_dataset, sort_field, {spatial_sort_method})&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 23 Jun 2015 18:30:36 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/sort-grid-text-field-sort-of-like-a-number/m-p/598376#M46816</guid>
      <dc:creator>SepheFox</dc:creator>
      <dc:date>2015-06-23T18:30:36Z</dc:date>
    </item>
    <item>
      <title>Re: Sort grid (text) field sort of like a number!</title>
      <link>https://community.esri.com/t5/python-questions/sort-grid-text-field-sort-of-like-a-number/m-p/598377#M46817</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;To add to&amp;nbsp; &lt;A _jive_internal="true" href="https://community.esri.com/people/sephefox"&gt;Sephe Fox&lt;/A&gt;​ said you'll need an advanced license and use the "Shape" as a sort field&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 23 Jun 2015 18:39:47 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/sort-grid-text-field-sort-of-like-a-number/m-p/598377#M46817</guid>
      <dc:creator>WesMiller</dc:creator>
      <dc:date>2015-06-23T18:39:47Z</dc:date>
    </item>
    <item>
      <title>Re: Sort grid (text) field sort of like a number!</title>
      <link>https://community.esri.com/t5/python-questions/sort-grid-text-field-sort-of-like-a-number/m-p/598378#M46818</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;That sounds great too. I only have a Standard license right now but I'll have to keep that in mind for some day in the future. Thanks!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 23 Jun 2015 18:53:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/sort-grid-text-field-sort-of-like-a-number/m-p/598378#M46818</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2015-06-23T18:53:29Z</dc:date>
    </item>
    <item>
      <title>Re: Sort grid (text) field sort of like a number!</title>
      <link>https://community.esri.com/t5/python-questions/sort-grid-text-field-sort-of-like-a-number/m-p/598379#M46819</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I'm a little confused what step you're at. Do you need to assign the page numbers based on whether or not it's a detail map, or create a sortable field to order your exported data driven pages based on the page number, or both?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 23 Jun 2015 20:39:18 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/sort-grid-text-field-sort-of-like-a-number/m-p/598379#M46819</guid>
      <dc:creator>DarrenWiens2</dc:creator>
      <dc:date>2015-06-23T20:39:18Z</dc:date>
    </item>
    <item>
      <title>Re: Sort grid (text) field sort of like a number!</title>
      <link>https://community.esri.com/t5/python-questions/sort-grid-text-field-sort-of-like-a-number/m-p/598380#M46820</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Well, what I do when I do it manually, is first I sort it as best I can. Then I make a number field and type in one by one, the number of the page. So that when I export data driven pages it has the right page number and it's in the right order. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So if I can sort this grid number field and then assign the page numbers to that sort order, I think I would be done. At one point in time I had some code that would assign a sequential number after I had it sorted how I wanted it.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Does that help explain?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 23 Jun 2015 20:50:15 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/sort-grid-text-field-sort-of-like-a-number/m-p/598380#M46820</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2015-06-23T20:50:15Z</dc:date>
    </item>
    <item>
      <title>Re: Sort grid (text) field sort of like a number!</title>
      <link>https://community.esri.com/t5/python-questions/sort-grid-text-field-sort-of-like-a-number/m-p/598381#M46821</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Yes, that helps. Do you have a quarter section grid? If so, it would be a matter of&lt;/P&gt;&lt;P&gt;1.) Create one field to store map type in your index layer&lt;/P&gt;&lt;P&gt;2.) Assign values of "TWP", "SEC", or "QTR" to denote whether the page is a township, section or quarter section map. You can likely do this automatically based on the index feature size (township &amp;gt; section &amp;gt; quarter).&lt;/P&gt;&lt;P&gt;3.) Spatial Join (must use the &lt;A href="http://resources.arcgis.com/en/help/main/10.1/index.html#//00080000000q000000"&gt;geoprocessing tool&lt;/A&gt;, not right-click join dialog) the index layer to the quarter section grid, based on HAVE_THEIR_CENTERS_IN. Now index features will have quarter info.&lt;/P&gt;&lt;P&gt;4.) Using if...then logic in field calculator, calculate the page number (if map type = "QTR", make a page number like "TWP" + "-" + "SEC" + "QTR")&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Once you have your page numbers in a field, you must determine their order, which will be my next post.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 23 Jun 2015 21:06:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/sort-grid-text-field-sort-of-like-a-number/m-p/598381#M46821</guid>
      <dc:creator>DarrenWiens2</dc:creator>
      <dc:date>2015-06-23T21:06:41Z</dc:date>
    </item>
    <item>
      <title>Re: Sort grid (text) field sort of like a number!</title>
      <link>https://community.esri.com/t5/python-questions/sort-grid-text-field-sort-of-like-a-number/m-p/598382#M46822</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;This ended up being more complicated than I expected, but here's what I got. I have doubt there's a more direct route. Also, there be some hidden gotchas in your data. It updates a field called "ord", based on labels in "rnd" (in format "102-65NE"), from a feature class "points":&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&amp;gt;&amp;gt;&amp;gt; from operator import itemgetter # import itergetter function
... myList = [] # make empty list
... fc = "points"
... labelField = "rnd"
... orderField = "ord"
... with arcpy.da.SearchCursor(fc,["OID@",labelField]) as cursor: # loop through your feature class
...&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in cursor:
...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; twp = str(row[1].split("-")[0]) # get township
...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if len(row[1].split("-")) &amp;gt; 1: # if there is more than just TWP
...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sec = ""
...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; qtr = ""
...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for x in row[1].split("-")[1]: # loop through characters after hyphen
...&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; if x.isdigit():
...&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sec += str(x) # add to section if numerical
...&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; else:
...&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; qtr += str(x) # add to quarter if letter
...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if len(qtr) == 0:
...&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; qtr = 0
...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sec = 0
...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print twp
...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; myList.append({"OID":row[0],"TWP":int(twp),"SEC":int(sec),"QTR":qtr}) # add to list
... myList = sorted(myList, key=itemgetter("TWP","SEC","QTR")) # sort list in place, by TWP, SEC, QTR
... with arcpy.da.UpdateCursor(fc,["OID@",orderField]) as cursor: # start updating
...&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in cursor:
&lt;SPAN style="line-height: 1.5;"&gt;...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row[1] = next(index for (index, d) in enumerate(myList) if d["OID"] == row[0]) # enumerate list and match OIDs&lt;/SPAN&gt;
...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cursor.updateRow(row) # update row to include a number&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;IMG __jive_id="112886" alt="1.PNG" class="image-1 jive-image" src="https://community.esri.com/legacyfs/online/112886_1.PNG" style="height: auto;" /&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 01:41:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/sort-grid-text-field-sort-of-like-a-number/m-p/598382#M46822</guid>
      <dc:creator>DarrenWiens2</dc:creator>
      <dc:date>2021-12-12T01:41:51Z</dc:date>
    </item>
    <item>
      <title>Re: Sort grid (text) field sort of like a number!</title>
      <link>https://community.esri.com/t5/python-questions/sort-grid-text-field-sort-of-like-a-number/m-p/598383#M46823</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;So I'm getting Parsing error SyntaxError: invalid syntax (line &lt;span class="lia-unicode-emoji" title=":smiling_face_with_sunglasses:"&gt;😎&lt;/span&gt; &lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code _jivemacro_uid_14351703143726116 jive_text_macro" data-renderedposition="29_8_913_16" jivemacro_uid="_14351703143726116"&gt;&lt;P&gt;twp = str(row[1].split("-")[0]) # get township&amp;nbsp; &lt;/P&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp; &lt;/P&gt;&lt;P&gt;What can I do to try and run this?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thank you for your effort to help! This code will probably help a lot of people.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 24 Jun 2015 18:27:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/sort-grid-text-field-sort-of-like-a-number/m-p/598383#M46823</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2015-06-24T18:27:03Z</dc:date>
    </item>
    <item>
      <title>Re: Sort grid (text) field sort of like a number!</title>
      <link>https://community.esri.com/t5/python-questions/sort-grid-text-field-sort-of-like-a-number/m-p/598384#M46824</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I'm not sure, that looks correct to me. The line says, "Find the value in the current row, in the field specified in the 2nd variable listed in the cursor (the labelField), split it when there is a hyphen, return the portion before the first hyphen (or the entire string if there is no hyphen), convert it to a string, and store the value in a variable called twp".&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Have you changed the labelField to match your field name? Also, check the previous line to make sure it's correct (e.g. not missing the colon).&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 24 Jun 2015 18:37:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/sort-grid-text-field-sort-of-like-a-number/m-p/598384#M46824</guid>
      <dc:creator>DarrenWiens2</dc:creator>
      <dc:date>2015-06-24T18:37:58Z</dc:date>
    </item>
    <item>
      <title>Re: Sort grid (text) field sort of like a number!</title>
      <link>https://community.esri.com/t5/python-questions/sort-grid-text-field-sort-of-like-a-number/m-p/598385#M46825</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;Runtime error 
Traceback (most recent call last):
&amp;nbsp; File "&amp;lt;string&amp;gt;", line 34, in &amp;lt;module&amp;gt;
ValueError: invalid literal for int() with base 10: '49 &amp;amp; 49A'
&lt;/PRE&gt;&lt;P&gt;Ok, I cleaned up the spacing in PyScripter. It does work on a subset of my data! &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;But I have really messy data in here. Like "49 &amp;amp; 49A" and "51A", among other names. Maybe there can be some code that just says to ignore those if it finds them? Because I'll have to do those manually anyway based on the old book order.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thank you!! &lt;IMG src="https://community.esri.com/legacyfs/online/emoticons/happy.png" /&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 01:41:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/sort-grid-text-field-sort-of-like-a-number/m-p/598385#M46825</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-12-12T01:41:54Z</dc:date>
    </item>
    <item>
      <title>Re: Sort grid (text) field sort of like a number!</title>
      <link>https://community.esri.com/t5/python-questions/sort-grid-text-field-sort-of-like-a-number/m-p/598386#M46826</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Sure, you can play around with .replace(). This will replace all " &amp;amp; " with "-", stored in variable newString:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;...
for row in cursor:
&amp;nbsp;&amp;nbsp;&amp;nbsp; newString = row[1].replace(" &amp;amp; ", "-")&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 01:41:57 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/sort-grid-text-field-sort-of-like-a-number/m-p/598386#M46826</guid>
      <dc:creator>DarrenWiens2</dc:creator>
      <dc:date>2021-12-12T01:41:57Z</dc:date>
    </item>
  </channel>
</rss>

