Python sorting a nested list with conditional comparison
<p>I am trying to sort a nested list <code>A</code>. <code>len(A) = n</code> and <code>len(A[i]) = d</code> for all <code>i</code>. I would like to sort using the first element of <code>A[i]</code>. But if <code>A[i][0] == A[j][0]</code> then I want to sort using the next element, i.e., <code>A[i][1]</code>. If <code>A[i][1] == A[j][1]</code> then use the next element, <code>A[i][2]</code>, and so on. </p>

<p>Here is an example with <code>n = 4</code> and <code>d = 2</code>. Because <code>[3,6]</code> and <code>[3,7]</code> have the same first element, they are compared based on the second element.</p>

<pre><code>A = [[3,7], [4,5], [3, 6], [5,1]]
A_sorted = [[3,6], [3,7], [4,5], [5,1]]
</code></pre>

<p>In Python 2.7 I used custom comparison function. 
<code>A.sort(cmp=comp_func)</code>. But I am trying to do this in Python 3 which does not have the <code>cmp</code> argument option. So I need to use <code>key</code> argument instead. How do I implement this custom sorting in Python 3?</p>

<p>This is the default Python sorting behavior.  Have you tried doing this and it hasn't worked?</p>

python python-3.x sorting key