Sorting a List based on dates
<p>Below is a code which I wrote to sort the elements of a list based on certain parameters. One the parameters is a date. Now the date format is mm/dd/yy. Now if the year is the same, I dont face any problem. However, if I change the year from 2011 to 2012 of a particular element, the solution breaks down. For example it treats 03/15/2012 as earlier to 03/18/2011. How do i get over this. I know changing the date format to yy/mm/dd will give me the correct solution but I don't want to tinker with that. Plz suggest a way out.</p>

<pre><code>from operator import itemgetter
import datetime

List=[['G1','E','03/12/2011',2],
      ['G2','E','03/10/2011',2],
      ['G3','2','03/19/2011',1],
      ['G4','2','03/15/2011',2],
      ['G6','2','03/15/2011',2]]
print List

List_expedite=[]
for element in List:
    if element[1]=='E':
        List_expedite.append(element)
print "Expedite List", List_expedite

List_non_expedite=[]
for element in List:
    if element[1]!='E':
        List_non_expedite.append(element)
print "Non-expedite List", List_non_expedite

List_expedite_sort=sorted(List_expedite, key=itemgetter(-1,-2))
print "Sorted expedite List",List_expedite_sort

List_non_expedite_sort=sorted(List_non_expedite,
                              key=lambda x: (x[-1],-int(x[-3]),x[-2]))
print "Sorted non-expedite List", List_non_expedite_sort
</code></pre>
