Grade

is a pair of ambivalent primitive functions related to sorting. Instead of sorting the given array directly, Grade returns a permutation vector whose length equals the number of major cells, so that indexing into the argument gives the sorted array. The glyph  is called Grade Up and gives the ascending sort order, while   is called Grade Down and gives the descending sort order.

Monadic form
Monadic Grade returns the sorting order of the major cells of the argument. Indexing into the original argument gives the sorted array; indexing into another array gives the result commonly known as Sort By.

Grade performs stable sorting, so that the indices of the repeated major cells are sorted by the order of appearance. Because of this, Grade Down produces the reverse of Grade Up only if all the major cells are unique.

Dyalog APL supports total array ordering when comparing nested arrays of possibly different shape and type. Grade is the only built-in function which can perform comparisons between arbitrary arrays of previously unknown order (Interval Index requires the left argument to be sorted beforehand).

Dyadic form
Dyadic Grade is limited to simple character array arguments. The left argument specifies the collation order to use when sorting the right argument.

If the left argument is a vector, the sorting order is simply the order of appearance in the left argument. In other words,  equals , and same for.

If the left argument is a higher-rank array, sorting is done in multiple stages. The last axis of X takes highest precedence, then the next-to-last, and so on, giving ties to all characters that appear at the same index over the current axis. A notable application is case-insensitive sorting.

J does not implement dyadic grade, but provides Sort By as the dyadic counterparts of  and   instead.

History
In A Programming Language, Grade is referred to as the ordering of a vector and written $$\theta_j/x$$ for vector $$x$$ and index origin index origin $$j$$: a special use of the slash that otherwise indicates Compress. The requirement that indices for equal elements should retain their indices (stable sorting) is mentioned explicitly.

Grade up and down were defined with the standard glyphs  and index origin-dependent definition in APL\360. By 1968 it had been defined to grade along a particular axis, defaulting to the last; however, this extension has often not been maintained in later APLs.

In 1980 SHARP APL extended Grade to sort the major cells of its argument, and introduced the dyadic Grade functions, following a suggestion by Howard Smith of IBM motivated by string sorting. While sorting cells is consistent with leading axis theory, it predates the beginning of this model's development in 1982. The extension to monadic Grade is incompatible with APL\360's definition of grading along an axis. Both extensions were widely adopted at the time, for example in the first versions of Dyalog APL. Newer dialects such as Kap often don't implement dyadic Grade (which was also left out of J in favor of Sort by).

Documentation

 * Dyalog Grade Up Monadic, Grade Down Monadic, Grade Up Dyadic, Grade Down Dyadic
 * APLX Grade Up, Grade Down
 * J Dictionary Grade Up, Grade Down, NuVoc Grade
 * BQN

Other

 * APL Cultivation lesson
 * Dyadic Grade by Roger Hui, Dyalog Blog
 * abrudz/Sort library: Examples of sorting variations (Classic, Natural, Danish, Finnish, German) using dyadic grade