docs: protected List<DbEntityOperation> sortByReferences(SortedSet<DbEntityOperation> preSorted) {
      // copy the pre-sorted set and apply final sorting to list
      List<DbEntityOperation> opList = new ArrayList<DbEntityOperation>(preSorted);
  
      for (int i = 0; i < opList.size(); i++) {
  
        DbEntityOperation currentOperation = opList.get(i);
        DbEntity currentEntity = currentOperation.getEntity();
        Set<String> currentReferences = currentOperation.getFlushRelevantEntityReferences();
  
        // check whether this operation must be placed after another operation
        int moveTo = i;
        for(int k = i+1; k < opList.size(); k++) {
          DbEntityOperation otherOperation = opList.get(k);
          DbEntity otherEntity = otherOperation.getEntity();
          Set<String> otherReferences = otherOperation.getFlushRelevantEntityReferences();
  
          if(currentOperation.getOperationType() == INSERT) {
  
  
            // if we reference the other entity, we need to be inserted after that entity
            if(currentReferences != null && currentReferences.contains(otherEntity.getId())) {
              moveTo = k;
              break; // we can only reference a single entity
            }
  
          } else { // UPDATE or DELETE
  
            // if the other entity has a reference to us, we must be placed after the other entity
            if(otherReferences != null && otherReferences.contains(currentEntity.getId())) {
              moveTo = k;
              // cannot break, there may be another entity further to the right which also references us
            }
  
          }
        }
  
        if(moveTo > i) {
          opList.remove(i);
          opList.add(moveTo, currentOperation);
          i--;
        }
      }
  
      return opList;
    }