<?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: How can I naturally sort alphanumeric values in a string? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/how-can-i-naturally-sort-alphanumeric-values-in-a/m-p/445933#M34969</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;permanent sorting would still require a key.&amp;nbsp; You can generate a key value pair by splitting the letters from the numbers.&amp;nbsp; By design, it is best to account for possibilities like this up front just by having your alphanumeric key formed so they were the same length.&amp;nbsp; You could use double letters, and triple numbers for example.&amp;nbsp; One advantage of this type of key is that they are readily sorted, but the letter component could be used as a subclassification system, making it easier to remember that EA0000 to EZ9999 belong to one group with 26 subgroups of 10000 each.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 29 Jul 2016 21:25:55 GMT</pubDate>
    <dc:creator>DanPatterson_Retired</dc:creator>
    <dc:date>2016-07-29T21:25:55Z</dc:date>
    <item>
      <title>How can I naturally sort alphanumeric values in a string?</title>
      <link>https://community.esri.com/t5/python-questions/how-can-i-naturally-sort-alphanumeric-values-in-a/m-p/445928#M34964</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;My field had values like B3, M4, S5, E26, E10, E9, E11 (all within one field).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I need to naturally sort so the values look like this: B3, E9, E10, E11, E26, M4, S5&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I've tried the following code as &lt;A href="https://arcpy.wordpress.com/2012/05/11/sorting-alphanumeric-strings-in-python/"&gt;suggested here&lt;/A&gt;, but it gives me an error stating "The value type is incompatible with the field type". If anyone has some suggestions I'd be interested! BTW I used this in Field Calculator&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;DIV class="number1 alt2 line index0"&gt;&lt;CODE class="keyword python"&gt;import&lt;/CODE&gt; &lt;CODE class="python plain"&gt;re&lt;/CODE&gt;&lt;P&gt;&lt;/P&gt;&lt;P class="number2 alt1 line index1"&gt;&lt;CODE class="keyword python"&gt;def&lt;/CODE&gt; &lt;CODE class="python plain"&gt;sorted_nicely( l ):&lt;/CODE&gt;&lt;/P&gt;&lt;P class="number3 index2 alt2 line"&gt;&lt;CODE class="spaces python"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/CODE&gt;&lt;CODE class="python comments"&gt;""" Sorts the given iterable in the way that is expected.&lt;/CODE&gt;&lt;/P&gt;&lt;P class="index3 number4 alt1 line"&gt;&lt;/P&gt;&lt;P class="index4 alt2 line number5"&gt;&lt;CODE class="spaces python"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/CODE&gt;&lt;CODE class="python comments"&gt;Required arguments:&lt;/CODE&gt;&lt;/P&gt;&lt;P class="index5 alt1 line number6"&gt;&lt;CODE class="spaces python"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/CODE&gt;&lt;CODE class="python comments"&gt;l -- The iterable to be sorted.&lt;/CODE&gt;&lt;/P&gt;&lt;P class="number7 alt2 index6 line"&gt;&lt;/P&gt;&lt;P class="index7 number8 alt1 line"&gt;&lt;CODE class="spaces python"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/CODE&gt;&lt;CODE class="python comments"&gt;"""&lt;/CODE&gt;&lt;/P&gt;&lt;P class="number9 alt2 line index8"&gt;&lt;CODE class="spaces python"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/CODE&gt;&lt;CODE class="python plain"&gt;convert &lt;/CODE&gt;&lt;CODE class="keyword python"&gt;=&lt;/CODE&gt; &lt;CODE class="keyword python"&gt;lambda&lt;/CODE&gt; &lt;CODE class="python plain"&gt;text: &lt;/CODE&gt;&lt;CODE class="functions python"&gt;int&lt;/CODE&gt;&lt;CODE class="python plain"&gt;(text) &lt;/CODE&gt;&lt;CODE class="keyword python"&gt;if&lt;/CODE&gt; &lt;CODE class="python plain"&gt;text.isdigit() &lt;/CODE&gt;&lt;CODE class="keyword python"&gt;else&lt;/CODE&gt; &lt;CODE class="python plain"&gt;text&lt;/CODE&gt;&lt;/P&gt;&lt;P class="alt1 line index9 number10"&gt;&lt;CODE class="spaces python"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/CODE&gt;&lt;CODE class="python plain"&gt;alphanum_key &lt;/CODE&gt;&lt;CODE class="keyword python"&gt;=&lt;/CODE&gt; &lt;CODE class="keyword python"&gt;lambda&lt;/CODE&gt; &lt;CODE class="python plain"&gt;key: [convert(c) &lt;/CODE&gt;&lt;CODE class="keyword python"&gt;for&lt;/CODE&gt; &lt;CODE class="python plain"&gt;c &lt;/CODE&gt;&lt;CODE class="keyword python"&gt;in&lt;/CODE&gt; &lt;CODE class="python plain"&gt;re.split(&lt;/CODE&gt;&lt;CODE class="string python"&gt;'([0-9]+)'&lt;/CODE&gt;&lt;CODE class="python plain"&gt;, key)]&lt;/CODE&gt;&lt;/P&gt;&lt;P class="alt2 index10 line number11"&gt;&lt;CODE class="spaces python"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/CODE&gt;&lt;CODE class="keyword python"&gt;return&lt;/CODE&gt; &lt;CODE class="functions python"&gt;sorted&lt;/CODE&gt;&lt;CODE class="python plain"&gt;(l, key &lt;/CODE&gt;&lt;CODE class="keyword python"&gt;=&lt;/CODE&gt; &lt;CODE class="python plain"&gt;alphanum_key)&lt;/CODE&gt;&lt;/P&gt;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 28 Jul 2016 18:26:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-can-i-naturally-sort-alphanumeric-values-in-a/m-p/445928#M34964</guid>
      <dc:creator>RobBlash</dc:creator>
      <dc:date>2016-07-28T18:26:03Z</dc:date>
    </item>
    <item>
      <title>Re: How can I naturally sort alphanumeric values in a string?</title>
      <link>https://community.esri.com/t5/python-questions/how-can-i-naturally-sort-alphanumeric-values-in-a/m-p/445929#M34965</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Do you always have 1 letter followed by X numbers?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 28 Jul 2016 18:28:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-can-i-naturally-sort-alphanumeric-values-in-a/m-p/445929#M34965</guid>
      <dc:creator>DarrenWiens2</dc:creator>
      <dc:date>2016-07-28T18:28:00Z</dc:date>
    </item>
    <item>
      <title>Re: How can I naturally sort alphanumeric values in a string?</title>
      <link>https://community.esri.com/t5/python-questions/how-can-i-naturally-sort-alphanumeric-values-in-a/m-p/445930#M34966</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;import re
def sorted_nicely( l ):
&amp;nbsp;&amp;nbsp;&amp;nbsp; """ Sorts the given iterable in the way that is expected.
&amp;nbsp;&amp;nbsp;&amp;nbsp; Required arguments:
&amp;nbsp;&amp;nbsp;&amp;nbsp; l -- The iterable to be sorted.
&amp;nbsp;&amp;nbsp;&amp;nbsp; """
&amp;nbsp;&amp;nbsp;&amp;nbsp; convert = lambda text: int(text) if text.isdigit() else text
&amp;nbsp;&amp;nbsp;&amp;nbsp; alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)]
&amp;nbsp;&amp;nbsp;&amp;nbsp; return sorted(l, key = alphanum_key)
if __name__ == "__main__":
&amp;nbsp;&amp;nbsp;&amp;nbsp; """ test """
&amp;nbsp;&amp;nbsp;&amp;nbsp; vals = [ 'B3', 'M4', 'S5', 'E26', 'E10', 'E9', 'E11' ]
&amp;nbsp;&amp;nbsp;&amp;nbsp; ret = sorted_nicely( vals )
&amp;nbsp;&amp;nbsp;&amp;nbsp; print("input {}\n sorted {}".format(vals, ret))&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;seems to work&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;runfile('C:/mystuff.....)
input ['B3', 'M4', 'S5', 'E26', 'E10', 'E9', 'E11']
sorted ['B3', 'E9', 'E10', 'E11', 'E26', 'M4', 'S5']
In [3]:&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;but to get it to work in the field calculator will be messy, it may be easier to dump the field out to an array, with the FID or OBJECTID field, sort the field in question and send the results back to join to the original file.&amp;nbsp; or some ugly cursor magic to read the field first or make a new field, split your existing data and pad the numbers&lt;/P&gt;&lt;P&gt;It is always best to use same width formatting ... E00, E01etc&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 19:55:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-can-i-naturally-sort-alphanumeric-values-in-a/m-p/445930#M34966</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2021-12-11T19:55:54Z</dc:date>
    </item>
    <item>
      <title>Re: How can I naturally sort alphanumeric values in a string?</title>
      <link>https://community.esri.com/t5/python-questions/how-can-i-naturally-sort-alphanumeric-values-in-a/m-p/445931#M34967</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;An alternate if you want to create a new field for sorting later&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;def make_sort(l):
&amp;nbsp;&amp;nbsp;&amp;nbsp; """make the field and sort"""
&amp;nbsp;&amp;nbsp;&amp;nbsp; for i in l:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print("{}{:0&amp;gt;3.0f}".format(i[0],float(i[1:])))
if __name__ == "__main__":
&amp;nbsp;&amp;nbsp;&amp;nbsp; """ test """
&amp;nbsp;&amp;nbsp;&amp;nbsp; vals = [ 'B3', 'M4', 'S5', 'E26', 'E10', 'E9', 'E11' ]
&amp;nbsp;&amp;nbsp;&amp;nbsp; make_sort(vals)&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Will make these results... you just have to 'field calculator it'&amp;nbsp; ie.&amp;nbsp; make_sort('youroriginalfield')&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;your new field
B003
M004
S005
E026
E010
E009
E011&lt;/PRE&gt;&lt;P&gt;Then just sort your new field&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 19:55:57 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-can-i-naturally-sort-alphanumeric-values-in-a/m-p/445931#M34967</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2021-12-11T19:55:57Z</dc:date>
    </item>
    <item>
      <title>Re: How can I naturally sort alphanumeric values in a string?</title>
      <link>https://community.esri.com/t5/python-questions/how-can-i-naturally-sort-alphanumeric-values-in-a/m-p/445932#M34968</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Before talking about the Field Calculator or this cursor or that cursor, what exactly are you trying to accomplish?&amp;nbsp; I see you have a field with some mixed alphanumeric numbers, and that you want them sorted "naturally."&amp;nbsp; Why do you want them sorted?&amp;nbsp; Is it for display purposes, analysis, something else?&amp;nbsp; Are you trying to permanently reorder the records in the table?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 28 Jul 2016 20:06:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-can-i-naturally-sort-alphanumeric-values-in-a/m-p/445932#M34968</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2016-07-28T20:06:11Z</dc:date>
    </item>
    <item>
      <title>Re: How can I naturally sort alphanumeric values in a string?</title>
      <link>https://community.esri.com/t5/python-questions/how-can-i-naturally-sort-alphanumeric-values-in-a/m-p/445933#M34969</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;permanent sorting would still require a key.&amp;nbsp; You can generate a key value pair by splitting the letters from the numbers.&amp;nbsp; By design, it is best to account for possibilities like this up front just by having your alphanumeric key formed so they were the same length.&amp;nbsp; You could use double letters, and triple numbers for example.&amp;nbsp; One advantage of this type of key is that they are readily sorted, but the letter component could be used as a subclassification system, making it easier to remember that EA0000 to EZ9999 belong to one group with 26 subgroups of 10000 each.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 29 Jul 2016 21:25:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-can-i-naturally-sort-alphanumeric-values-in-a/m-p/445933#M34969</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2016-07-29T21:25:55Z</dc:date>
    </item>
    <item>
      <title>Re: How can I naturally sort alphanumeric values in a string?</title>
      <link>https://community.esri.com/t5/python-questions/how-can-i-naturally-sort-alphanumeric-values-in-a/m-p/445934#M34970</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;My initial question was not clear, my apologies. I'm creating a map book street index, for display purposed only. All of those values are within a string for a single record, so I'm trying to sort within that one string. I can alter the grid to add leading zeros (A01 instead of A1) but I'd rather avoid that if possible. My screen shot doesn't show any numbers smaller &amp;lt;10 but they're mixed in here and there.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;IMG alt="StreetIndex.PNG" class="image-1 jive-image" src="https://community.esri.com/legacyfs/online/214082_StreetIndex.PNG" style="height: auto;" /&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 01 Aug 2016 11:45:27 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-can-i-naturally-sort-alphanumeric-values-in-a/m-p/445934#M34970</guid>
      <dc:creator>RobBlash</dc:creator>
      <dc:date>2016-08-01T11:45:27Z</dc:date>
    </item>
    <item>
      <title>Re: How can I naturally sort alphanumeric values in a string?</title>
      <link>https://community.esri.com/t5/python-questions/how-can-i-naturally-sort-alphanumeric-values-in-a/m-p/445935#M34971</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;def srt(val):
&amp;nbsp;&amp;nbsp;&amp;nbsp; """split and sort"""
&amp;nbsp;&amp;nbsp;&amp;nbsp; old = val.split(", ")
&amp;nbsp;&amp;nbsp;&amp;nbsp; new = ["{}{:0&amp;gt;2.0f}".format(i[0],int(i[1:]))&amp;nbsp; for i in old]
&amp;nbsp;&amp;nbsp;&amp;nbsp; new.sort()
&amp;nbsp;&amp;nbsp;&amp;nbsp; out = ", ".join([i for i in new])
&amp;nbsp;&amp;nbsp;&amp;nbsp; return out
&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This example&lt;/P&gt;&lt;P&gt;val = "B3, M4, S5, E26, E10, E9, E11"&lt;/P&gt;&lt;P&gt;returns this&lt;/P&gt;&lt;P&gt;'B03, E09, E10, E11, E26, M04, S05'&lt;/P&gt;&lt;P&gt;is that better?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 19:55:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-can-i-naturally-sort-alphanumeric-values-in-a/m-p/445935#M34971</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2021-12-11T19:55:59Z</dc:date>
    </item>
    <item>
      <title>Re: How can I naturally sort alphanumeric values in a string?</title>
      <link>https://community.esri.com/t5/python-questions/how-can-i-naturally-sort-alphanumeric-values-in-a/m-p/445936#M34972</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Perfect, thanks!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 01 Aug 2016 12:17:06 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-can-i-naturally-sort-alphanumeric-values-in-a/m-p/445936#M34972</guid>
      <dc:creator>RobBlash</dc:creator>
      <dc:date>2016-08-01T12:17:06Z</dc:date>
    </item>
  </channel>
</rss>

