skip to main content
Corticon Studio: Rule Modeling Guide : Collections : Using sorts to find the first or last in grandchild collections
 

Try Corticon Now

Using sorts to find the first or last in grandchild collections

The SortedBy->first and SortedBy->last constructs work as expected for any first-level collection regardless of datatype, determining the value of the first or last element in a sequence that was derived from a collection.
When associations are involved, you have to take care that the collection operator is not working at a grandchild level. You could construct a single collection of multiple children (rather than multiple collections of a single child) by “bubbling up” the relevant value into the child level, and then sort at that level. Another technique is to change the scope to treat the root level entity as the collection, and then apply filters so that only the ones matching the common attribute values across the associations are considered, and when you apply SortedBy->first or SortedBy->last, the intended value is the result.