擅长:python、mysql、java
<p>你要找的是所谓的“自然排序”。这与“词典分类”相反。有几种方法可以做到这一点,因为您想要的确切输出是特定于实现的。快速的google搜索会产生这样的结果(注意*这不是我的代码,也不是我测试过的代码):</p>
<pre><code>import re
def tryint(s):
try:
return int(s)
except:
return s
def alphanum_key(s):
""" Turn a string into a list of string and number chunks.
"z23a" -> ["z", 23, "a"]
"""
return [ tryint(c) for c in re.split('([0-9]+)', s) ]
def sort_nicely(l):
""" Sort the given list in the way that humans expect.
"""
l.sort(key=alphanum_key)
</code></pre>
<p><a href="http://nedbatchelder.com/blog/200712.html#e20071211T054956" rel="nofollow">http://nedbatchelder.com/blog/200712.html#e20071211T054956</a></p>