Sorting strings with numbers in Python
<p>I have this string: </p>

<pre><code>string = '9x3420aAbD8'
</code></pre>

<p>How can I turn string into:</p>

<pre><code>'023489ADabx'
</code></pre>

<p>What function would I use?</p>

<p>You can just use the built-in function <a href="https://docs.python.org/3.5/library/functions.html#sorted" rel="nofollow"><code>sorted</code></a> to sort the string lexographically. It takes in an iterable, sorts each element, then returns a sorted list. Per the documentation:</p>

<blockquote>
  <p><code>sorted(iterable[, key][, reverse])</code></p>
  
  <p>Return a new sorted list from the items in <em>iterable</em>.</p>
</blockquote>

<p>You can apply that like so:</p>

<pre><code>&gt;&gt;&gt; ''.join(sorted(string))
'023489ADabx'
</code></pre>

<p>Since <code>sorted</code> returns a list, like so:</p>

<pre><code>&gt;&gt;&gt; sorted(string)
['0', '2', '3', '4', '8', '9', 'A', 'D', 'a', 'b', 'x']
</code></pre>

<p>Just join them together to create the desired string.</p>

python string sorting