import re


def sort_special_vertex_groups(vgroups,
                               special_vertex_group_pattern='STYMO:',
                               global_special_vertex_group_suffix='Character'):
    """
    Given a list of special vertex group names, all with the prefix of
    special_vertex_group_pattern, selects all that start with global_special_vertex_group_suffix
    and puts them at the start of the list. This enables e.g. to easily define
    top-level vertex groups that always go first, followed by details that
    overwrite top level assignments.
    """
    global_vg_name_pattern = special_vertex_group_pattern + \
                             global_special_vertex_group_suffix
    first = []
    last = []
    for g in vgroups:
        if re.match(global_vg_name_pattern, g) is not None:
            first.append(g)
        else:
            last.append(g)

    first.sort()
    last.sort()
    first.extend(last)
    return first