wiki:python/sorted

Python Doc
Python HowTo


How to sort alpha numeric set in python
Liste nach Attributen sortieren...
reversed(enumerate())

Examples

operator/attrgetter

def sort_datetimes(dates, order=None):
    key = operator.attrgetter(order) if order else None
    return sorted(dates, key=key)

>>> dates = [datetime.datetime(2013, 1, 8), datetime.datetime(2012, 2, 5), datetime.datetime(2011, 3, 9)]
>>> sort_datetimes(dates)
[datetime.datetime(2011, 3, 9, 0, 0),
 datetime.datetime(2012, 2, 5, 0, 0),
 datetime.datetime(2013, 1, 8, 0, 0)]
>>> sort_datetimes(dates, order='day')
[datetime.datetime(2012, 2, 5, 0, 0),
 datetime.datetime(2013, 1, 8, 0, 0),
 datetime.datetime(2011, 3, 9, 0, 0)]
Last modified 6 years ago Last modified on Oct 15, 2013, 4:02:57 PM