private boolean addGrouping(List<Grouping> groups, Grouping group) throws AmbariException {
    if (group.addAfterGroup == null) {
      throw new AmbariException("Group " + group.name + " needs to specify which group it should come after");
    }
    else {
      // Check the current services, if the "after" service is there then add these
      for (int index = groups.size() - 1; index >= 0; index--) {
        String name = groups.get(index).name;
        if (name.equals(group.addAfterGroup)) {
          groups.add(index + 1, group);
          LOG.debug("Added group/after: {}/{}", group.name, group.addAfterGroup);
          return true;
        }
      }
    }
    return false;
  }
Adds the group provided if the group which it should come after has been added.
ambari-server/src/main/java/org/apache/ambari/server/stack/StackModule.java