pythonsortingdictionarydate-sorting

How to sort dates that are in tuples in python


I am trying to sort a dictionary of tuples where the second item contains the dates to be sorted. The dictionary looks something like this:

time_founded: {Soonr: 2005-5-1, SpePharm: 2006-9-1, and so on...}

Right now I am trying to sort the dates like this:

dict = sortedLists[category]    
sortedtime = sorted(dict.iteritems(),  key=lambda  d: map(int, d.split('-')))

But I am getting an error because it is trying to sort the tuples (Soonr: 2005-5-1) instead of just the date. How can I update the sorting parameters to tell it to only look at the date on not the whole tuple?


Solution

  • Try this:

    sortedtime = sorted(dict.iteritems(), key=lambda d: map(int, d[1].split('-')))
    

    The only difference is the [1] which selects out the value portion of the item.