def index_users_by_group(users):
    """
    Create an index of all users by their groups. Some users will appear multiple times
    depending on the groups they are assigned.
    """
    index = {}
    for user in users:
        for group in user.get("groups", []):
            index.setdefault(group, [])
            index[group].append(user)
    for group_name, user_group in index.items():
        user_group.sort(key=lambda user: (user.get("name") or "").upper())
    return index