You can create a special kind of alias in the Scope section of a Rulesheet. The technique uses the specialized Sequence operator ->next against a sorted alias (a special cached sequence) inside a filter expression. The Rulesheet is set into a Ruleflow that iterates to bind the alias in each successive invocation to the next element in the sequence.

The following example shows a Rulesheet based on the Cargo Vocabulary. The Cargo entity and its weight attribute were brought into the scope:



The operators sortedBy and sortedByDesc enable sorting in ascending or descending order of the numeric or alphabetic values of the attribute in the set of data. Note that an attribute with a Boolean data type is not valid for this operation.



Dragging the sortedBy operator and dropping it (you cannot type it in) on the attribute weight places it in the scope, yet an error shows:



The error message notes that a sorted alias node requires an alias name. When you enter an alias name, the scope is complete.



A filter expression is added to establish that, when you iterate through the list, each pass presents the next sequential item in the sorted set. You defined this by dragging sortedBy from the scope to filter line 1, and then appended the ->next operator. A rule statement based on sorted load that echoes the weight was added so you can see the results in the tests.

The Rulesheet is saved and a Ruleflow is created, adding in the Rulesheet. Then, you drag an Iterative operation to the Rulesheet in the Ruleflow and save it.



A Ruletest with a few Cargo items was created, each with a weight that is expected to sequence numerically when you run the test. Each iteration posts a message, and that message (the corresponding Rule Statement) contains the embedded attribute load weight. Because each member of the load collection will trigger the nonconditional rule, and even though the elements will be processed in no particular order, you expect to see a set of resulting messages with load weight in order. Running the tests repeatedly outputs the weights in ascending order every time.



If you change the operator to sortByDesc, the results are shown in descending order by weight, as expected.