code: public static List<ExecutionEntity> orderFromRootToLeaf(Collection<ExecutionEntity> executions) {
      List<ExecutionEntity> orderedList = new ArrayList<ExecutionEntity>(executions.size());
  
      // Root elements
      HashSet<String> previousIds = new HashSet<String>();
      for (ExecutionEntity execution : executions) {
        if (execution.getParentId() == null) {
          orderedList.add(execution);
          previousIds.add(execution.getId());
        }
      }
  
      // Non-root elements
      while (orderedList.size() < executions.size()) {
        for (ExecutionEntity execution : executions) {
          if (!previousIds.contains(execution.getId()) && previousIds.contains(execution.getParentId())) {
            orderedList.add(execution);
            previousIds.add(execution.getId());
          }
        }
      }
  
      return orderedList;
    }
docs: Takes in a collection of executions belonging to the same process instance. Orders the executions in a list, first elements are the leaf, last element is the root elements.