About the Sortings of Loops

Most of the time, you are interested by presenting the results in a certain order in your generated report, for example, in an alphabetical order. When sorting nested loops, you must place the sorting criterion at the right place and level.

Important: A sorting criterion must be applied at the parent level of nested loops. Otherwise, you can get an unexpected sorting of loops, because the order is provided by the upper loop.

This page discusses:

Sorting of a Simple Loop

If your template is a succession of nested loops, which return and receive variables (that do not matter) and if you want to sort on a terminal data, follow this recommendation.

Recommendation: Convert nested loops in a simple loop, by creating a shortcut method to be used to create a simple loop and insert your sorting criteria on this loop.

Accessing linked objects of the same nature such as products and subproducts, suits to this situation.

Example: Get Physical Products

In the following example, the template lets you get physical products and their subproducts. You want to provide them alphabetically by title.



If you place the sorting by reference title at the child level, the result is not the expecting one.

Create a shortcut method to define a single loop instead of two loops:

PACKAGE PLM;
METHOD VPMReference.getSubProductReferences() -doc : 
   SubProducts.GetReferencePLMReference[EACH ISA VPMReference] 
LABEL "SubProduct References";

You can use this method in your template, and then place the sorted title on this loop, as follows:

Sorting by Keeping Nested Loops

You cannot always simplify a structure of nested loops using a simple shortcut.

If you are in the following situation, you cannot create a simple shortcut:
  • The nesting of loops contain branches (that is a loop, which contains several loops for a level in the nesting that is interesting to be sorted).
  • The intermediate variables are important in the template.

To access information available at a lower level for sorting or filtering on a parent loop, create a specific method if you know how to get information from the lower level at the parent level or if the access is simple.

Recommendation: In these cases, you must follow a bottom-up approach to access terminal data and manage them at the higher level. To do so, you create a shortcut method to be inserted as a sorting criteria on the parent loop.

Example: Get Specification Structures

In the following example, the template lets you get the requirements, the chapters, and the comments of the specification structures, and provides them alphabetically.

Create a customized method to define a sorting criteria on titles:

PACKAGE PLM;
METHOD `rel_Specification Structure`.getChildTitle() -doc : 
   (to_Requirement.Title DEFAULT to_Chapter.Title) DEFAULT to_Comment.Title
LABEL "Child Title";

You can create a template as below, and then insert this sorting criteria at the parent level of the nested loops: