{order:ids:expression:type}

Description

Sorts a fixed list of item ids by an AA expression evaluated for each item, and returns the ids dash-joined in the new order. Give the ids dash-separated (the output of {ids} or any id-producing command works); the expression is computed once per item (usually a field alias such as _#NUMBER__ or _#HEADLINE) and its result is that item sort key. Choose the sort type: numeric (the default), rnumeric, string, rstring, locale or rlocale - the r-variants reverse the direction. Numeric and string sorting differ sharply: as numbers 7 sorts before 14 before 100, but as strings 100 sorts before 14 before 7 (compared character by character). Sorting inside the database (as {ids} does) is faster and preferred; reach for order only when you already hold a concrete id list that must be re-sorted by a non-database key. Built on {aggregate} with the order action.

Parameters

ids required default (empty - required)

The list of item ids to sort, separated by dashes. Long (32-char) or short ids both work, and you can feed the output of {ids} or another id-producing command straight in. An empty list produces an empty result.

expression optional default publish_date....

The AA expression evaluated once per item; its result is that item's sort key. Usually a field alias such as _#NUMBER__ or expression, but any expression works. All ids are compared by this single key.

type optional default numeric

How the sort keys are compared. numeric (the default) orders by size; string orders character by character; locale uses the current language's collation. Prefix r for reverse (descending) in each mode.

Examples

test{foreach:{order:aa89ddc95136b966e6ec1419a2963741-a65d19971986e750761f0efcd9a16b1b-5726c2c6b035d7aab450d1794e9e90d7-ae1da231f3e5bc16496834a0711aa042:_#NUMBER__}:{({item:_##1:_#HEADLINE})}:-:>}
ExpectedDelta>Alpha>Gamma>Kappa
ActualDelta>Alpha>Gamma>Kappa
The default sort type is numeric ascending, so the four items come back ordered by their _#NUMBER__ field: 7, 14, 100, 1000. order returns only the reordered ids; the {foreach} wrapper with the deferred-eval read {({item:_##1:_#HEADLINE})} prints each item's headline so the result is human-readable (_##1 is the current id in the loop).
test{foreach:{order:aa89ddc95136b966e6ec1419a2963741-a65d19971986e750761f0efcd9a16b1b-5726c2c6b035d7aab450d1794e9e90d7-ae1da231f3e5bc16496834a0711aa042:_#NUMBER__:rnumeric}:{({item:_##1:_#HEADLINE})}:-:>}
ExpectedKappa>Gamma>Alpha>Delta
ActualKappa>Gamma>Alpha>Delta
rnumeric reverses the numeric order, so the same four items run from the largest number down: 1000, 100, 14, 7.
test{foreach:{order:aa89ddc95136b966e6ec1419a2963741-a65d19971986e750761f0efcd9a16b1b-5726c2c6b035d7aab450d1794e9e90d7-ae1da231f3e5bc16496834a0711aa042:_#NUMBER__:string}:{({item:_##1:_#HEADLINE})}:-:>}
ExpectedGamma>Kappa>Alpha>Delta
ActualGamma>Kappa>Alpha>Delta
With type string the numbers are compared character by character, not by size: 100 sorts before 1000 before 14 before 7. This is the classic trap - use numeric when the field holds numbers.
test{foreach:{order:fb3df1343ef9a7d789af1f26aa5f8849-a65d19971986e750761f0efcd9a16b1b-ae1da231f3e5bc16496834a0711aa042-aa89ddc95136b966e6ec1419a2963741:_#HEADLINE:string}:{({item:_##1:_#HEADLINE})}:-:>}
ExpectedAlpha>Delta>Kappa>Theta
ActualAlpha>Delta>Kappa>Theta
The expression can be any field. Here _#HEADLINE is the sort key with type string, so the items come out in alphabetical order regardless of how they were listed.
test{foreach:{order:a65d19971986e750761f0efcd9a16b1b-4f3362a62847fe1f8c59ba28a92d42c0:_#NUMBER__}:{({item:_##1:_#HEADLINE})}:-:>}[{order::_#NUMBER__}]
ExpectedAlpha>Beta[]
ActualAlpha>Beta[]
Ordering a one- or two-element list just returns those ids in sorted order (Alpha 14 before Beta 42). An empty id list yields an empty result, shown here as the [] after the loop.
virtual{foreach:{order:{ids:6a435236626262738348478463536272:d-switch.........1-==-1}:_#SORTKEY_:string}:<option>_#NAME____</option>}
Expected(options in _#SORTKEY_ order)
The common pattern: fetch ids with {ids} (which can only sort by database fields), then {order} re-sorts that list by a computed or custom sort-key alias before you loop over them. Here the active rows of a slice are reordered by a _#SORTKEY_ field and rendered as select options. Marked illustrative because the output depends on that slice's live data.