擅长:python、mysql、java
<pre><code>>>> x = [4,5,81,5,28958,28] # first list
>>> print sorted(x)
[4, 5, 5, 28, 81, 28958]
>>> x
[4, 5, 81, 5, 28958, 28]
>>> x.sort() # sort the list in place
>>> x
[4, 5, 5, 28, 81, 28958]
>>> x.append(1) # add to the list
>>> x
[4, 5, 5, 28, 81, 28958, 1]
>>> sorted(x)
[1, 4, 5, 5, 28, 81, 28958]
</code></pre>
<p>正如许多其他人所指出的,您可以将一个数字向前排序如下:</p>
<pre><code>>>> int(''.join(sorted(str(2314))))
1234
</code></pre>
<p>这几乎是最标准的方法。</p>
<p>倒数?在带有尾随零的数字中不起作用。</p>
<pre><code>>>> y = int(''.join(sorted(str(2314))))
>>> y
1234
>>> int(str(y)[::-1])
4321
</code></pre>
<p><code>[::-1]</code>表示法表示iterable将以相反的顺序遍历。</p>