 <p>I use the following for sorting a 2d array on a number of columns</p> <pre><code>def k(a,b): def _k(item): return (item[a],item[b]) return _k </code></pre> <p>This could be extended to work on an arbitrary number of items. I tend to think finding a better access pattern to your sortable keys is better than writing a fancy comparator.</p> <pre><code>&gt;&gt;&gt; data = [[0,1,2,3,4],[0,2,3,4,5],[1,0,2,3,4]] &gt;&gt;&gt; sorted(data, key=k(0,1)) [[0, 1, 2, 3, 4], [0, 2, 3, 4, 5], [1, 0, 2, 3, 4]] &gt;&gt;&gt; sorted(data, key=k(1,0)) [[1, 0, 2, 3, 4], [0, 1, 2, 3, 4], [0, 2, 3, 4, 5]] &gt;&gt;&gt; sorted(a, key=k(2,0)) [[0, 1, 2, 3, 4], [1, 0, 2, 3, 4], [0, 2, 3, 4, 5]] </code></pre>