@contextfilter
def group_by_letter(context, object_list):
    "Group contacts by letter"
    res = {}

    for x in object_list:
        # case 1
        r = re.search('^[a-zA-Z]', x.name)
        if r:
            key = r.group().lower()
            if key not in res:
                # print "reg", key
                res[key] = [x]
            else:
                res[key].append(x)

        # case 2
        n = re.search('^[0-9_]', x.name)
        if n:
            if '#' not in res:
                res['#'] = [x]
            else:
                res['#'].append(x)

        # case 3
        if not n and not r:
            if '#' not in res:
                res['#'] = [x]
            else:
                res['#'].append(x)

    # converting dictionary to list of tuples, since template support only
    # List.
    l = []
    for k, v in res.items():
        l.append((k, v))
        l.sort(cmp=lambda x, y: cmp(x, y))
    return l
dataset/ETHPy150Open treeio/treeio/treeio/core/templatetags/modules.py/group_by_letter/original