Trying to sort a list of tuples in python first by date then by greatest number
<p>I have a list of tuples and I am trying to sort by date and then by the greatest number. So basically when you have two dates that are the same it will then put the tuple with the greatest number first. See example below. </p>

<p><strong>My list of tuples</strong></p>

<pre><code>dataLst = [["Mike", 50, "08/10/2016"], ["Bob", 100, "08/10/2016"], ["Dave", 500, "08/01/2016"], ["Paul", -50, "08/20/2016"], ["Sam", 250, "08/30/2016"]]
</code></pre>

<p><strong>I then sort the list by the dates in the tuples.</strong></p>

<pre><code>sDataLst = sorted(dataLst, key=operator.itemgetter(2))
</code></pre>

<p><strong>I then sort the sorted list by the numbers in reverse order.</strong></p>

<pre><code>sorted(sDataLst,key=operator.itemgetter(1), reverse=True) 
</code></pre>

<p><strong>When I print the sorted list I get the following.</strong></p>

<pre><code>print sDataLst

[['Dave', 500, '08/01/2016'], ['Mike', 50, '08/10/2016'], ['Bob', 100, '08/10/2016'], ['Paul', -50, '08/20/2016'], ['Sam', 250, '08/30/2016']]
</code></pre>

<p>If you look at the second and third item in the list which have the same date they should be reversed because the number in the third item is greater then the one in the second item. What I am looking for as a result is the following.</p>

<pre><code>[['Dave', 500, '08/01/2016'], ['Bob', 100, '08/10/2016'], ['Mike', 50, '08/10/2016'], ['Paul', -50, '08/20/2016'], ['Sam', 250, '08/30/2016']]
</code></pre>

<p>Any help on this would be greatly appreciated.</p>

<p>Thanks</p>

<p>You cannot compare the dates as strings because of the order <em>mm/dd/yyyy</em>, you could use <em>datetime</em> to :   </p>

<pre><code>from datetime import datetime
strp = datetime.strptime

srted = sorted(dataLst, key=lambda sub: (strp(sub[2],"%m/%d/%Y"), -sub[1]))
</code></pre>

<p>Or just split and reverse the order of the date to <em>yyyy, mm, dd:</em></p>

<pre><code>def iso(sub):
    mm, dd, yy = sub.split("/")
    return yy, mm, dd

srted = sorted(dataLst, key=lambda sub: (iso(sub[2]), -sub[1]))
</code></pre>

<p>Both give you the same output:</p>

<pre><code> In [24]: sorted(dataLst, key=lambda sub: (iso( sub[2]), -sub[1]))Out[24]: 
[['Dave', 500, '08/01/2016'],
 ['Bob', 100, '08/10/2016'],
 ['Mike', 50, '08/10/2016'],
 ['Paul', -50, '08/20/2016'],
 ['Sam', 250, '08/30/2016']]

In [25]: sorted(dataLst, key=lambda sub: (strp(sub[2],"%m/%d/%Y"), -sub[1]))

Out[25]: 
[['Dave', 500, '08/01/2016'],
 ['Bob', 100, '08/10/2016'],
 ['Mike', 50, '08/10/2016'],
 ['Paul', -50, '08/20/2016'],
 ['Sam', 250, '08/30/2016']]
</code></pre>

<p>The difference is splitting is way faster as you can see below:</p>

<pre><code>In [28]: timeit  sorted(dataLst, key=lambda sub: (strp(sub[2],"%m/%d/%Y"), -sub[1]))
10000 loops, best of 3: 66.4 Âµs per loop

In [29]: timeit sorted(dataLst, key=lambda sub: (iso( sub[2]), -sub[1]))
100000 loops, best of 3: 4.97 Âµs per loop
</code></pre>

python list sorting