<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def prev_next_group(project, group):
 """Return adjacent group objects or None for the given project and group. The previous and next group objects are relative to sort order of the project's groups with respect to the passed in group. """

    # TODO: Profile and optimize this query if necessary
    groups = sorted(x for x in project.groups if x.submissions)
    try:
        index = groups.index(group)
    except ValueError:
        return None, None
    prev_group = groups[index - 1] if index > 0 else None
    next_group = groups[index + 1] if index + 1 < len(groups) else None
    return prev_group, next_group