I can't use the sorted function to sort a two elements tuple list in descending numerical order then in ascending alphabetical order in case of a tie
<p>I used the sorted() function to sort a list of tuples, as it is shown below. the output is in descending order as wanted for the second elements (numbers). but what I really want is to sort the tuples in ascending alphabetical order in case of a tie.</p>

<p>Help please. </p>

<pre><code>sorted(word_Count, key = itemgetter(1,0), reverse = True)
</code></pre>

<p>Output: </p>

<pre><code>[('butter', 2), ('was', 1), ('the', 1), ('of', 1), ('but', 1),
('bought', 1), ('bitter', 1), ('bit', 1), ('betty', 1), ('a', 1)]
</code></pre>

<p>thank you</p>

<p>You can use the <code>sorted()</code> function along with <a href="http://www.diveintopython.net/power_of_introspection/lambda_functions.html" rel="nofollow"><code>lambda</code></a> as:</p>

<pre><code>&gt;&gt;&gt; sorted(word_count, key=lambda x: (-x[1], x[0]))
[('butter', 2), ('a', 1), ('betty', 1), ('bit', 1), ('bitter', 1), ('bought', 1), ('but', 1), ('of', 1), ('the', 1), ('was', 1)]
</code></pre>

<p>My <code>lambda</code> function is returning tuple as <code>(-x[1], x[0]))</code> which represents to sort the list in descending order of <code>x[1]</code> (<code>-</code> represents descending) and in case of match sort in the ascending order of <code>x[0]</code> (without <code>-</code> sign means ascending) </p>

python function sorting tuples