擅长:python、mysql、java
<p>为什么不使用<code>key</code>?在</p>
<pre><code>a = ['green egg','snail and lettuce','bacon','dorse naga','rutabaga ripple','cheese']
a.sort(key=len)
</code></pre>
<p>如果要对<code>cmp</code>使用旧式排序,则必须定义函数cmp<a href="https://wiki.python.org/moin/HowTo/Sorting/" rel="nofollow">like this</a>:</p>
<blockquote>
<p>That function should take two arguments to be compared and then return
a negative value for less-than, return zero if they are equal, or
return a positive value for greater-than</p>
</blockquote>
<p>所以在你的情况下可能是这样的:</p>
^{pr2}$
<p>此函数用于比较基于比较的排序中的两个元素(<a href="http://en.wikipedia.org/wiki/Timsort" rel="nofollow">Timsort</a>,如果我没弄错的话)</p>