def group_items(items, by=None, sorted_=True):
    if by is not None:
        pairs = list(zip(by, items))
        if sorted_:
            try:
                pairs = sorted(pairs, key=op.itemgetter(0))
            except TypeError:
                pairs = sorted(pairs, key=lambda tup: str(tup[0]))
    else:
        pairs = items
    groupid_to_items = defaultdict(list)
    for groupid, item in pairs:
        groupid_to_items[groupid].append(item)
    return groupid_to_items