Specify the sorting direction for the various columns/

Oni drentha at gmail.com
Sun Jun 14 23:53:39 EDT 2009


Thanks for the answers. My goal was to try to avoid hard coding
and add a little shine to the code I have inherited. But its
too far gone already and time is short.  So have used Mike's answer.

Mike answer with minor changes:

import datetime
import pprint
import operator
import time

entries = [{'name': 'ZZ2', 'username': 'ZZ3', 'date':
    datetime.datetime (2007, 9, 30, 16, 43, 54)},{'name': 'ZZ2',
    'username': 'ZZ5','date': datetime.datetime(2008, 9, 30, 16, 43,
    54)},{'name': 'ZZ3', 'username': 'ZZ1', 'date':
    datetime.datetime(2007, 9, 30, 16, 43, 54)}, {'name': 'AA2',
    'username': 'AA2','date': datetime.datetime(2007, 9, 30, 16, 43,
    54)}]

entries.sort(key = lambda x: (x['name'], -time.mktime(x
['date'].timetuple())   ))

pp = pprint.PrettyPrinter(depth=2)
pp.pprint(entries)




More information about the Python-list mailing list