Multidimensional sortedBy

Key: OCL25213

Status: open

Source: Model Driven Solutions ( Ed Willink)

Summary:
The sortedBy iteration provides an elegant solution to a sort problem in which the sort metric is a projection of the sorted object. Thus sortedBy(pp.name) or just sortedBy(name) is short and avoids the opportunities for typos from a more conventional exposition involving comparison of two objects. The natural solution may well be an efficient one for large collections with nontrivial metrics.
However the sortedBy solution is unfamiliar and so confusing for newcomers and unsuitable for multikey sorting for which an artificial compound single key may need to be constructed.
One solution might be to provide a more conventional iterator such as sort(p1, p2  comparisonexpression) allowing a two key sort:
sort(p1, p2  let diff1 = p1.key1.compareTo(p2.key1) in
if diff1 <> 0 the diff 1 else p1.key2.compareTo(p2.key2) endif)However this has poor readability and ample opportunities for typos.
Alternatively sortedBy with a Tuplevalued metric might support multiple keys as:
sortedBy(Tuple
{first=key1,second=key2})
(The alphabetical order of the Tuple part names determines the priority.)
(Since sortedBy is declaratively clear and compact, inefficient small/trivial implementations can be optimized to their sort() equivalents.)

Reported: OCL 2.4 — Sat, 20 Jan 2018 09:53 GMT

Updated: Sat, 20 Jan 2018 09:53 GMT