Like groupby(), but sorts the input on the group key first.

def full_groupby(iterable, key=None):
    """
    Like groupby(), but sorts the input on the group key first.
    """

    return groupby(sorted(iterable, key=key), key=key)