Using sorts to find the first or last in grandchild collections
- Last Updated: December 30, 2020
- 1 minute read
- Corticon
- Version 7.2
- Documentation
The SortedBy->first and SortedBy->last constructs work as expected for any
first-level collection regardless of data type, 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. When you apply SortedBy->first or SortedBy->last, the intended value is the result.