def sorted_groupby(seq, key):
    if not hasattr(key, '__call__'):
        key = operator.attrgetter(key)
    return itertools.groupby(sorted(seq, key=key), key=key)
Given a sequence, we sort it and group it by a key.
dataset/ETHPy150Open mozilla/addons-server/src/olympia/amo/utils.py/sorted_groupby