{"id":5706,"date":"2019-02-14T12:55:59","date_gmt":"2019-02-14T12:55:59","guid":{"rendered":"https:\/\/www.dyalog.com\/blog\/?p=5706"},"modified":"2019-02-14T14:15:20","modified_gmt":"2019-02-14T14:15:20","slug":"ascending-and-descending","status":"publish","type":"post","link":"https:\/\/www.dyalog.com\/blog\/2019\/02\/ascending-and-descending\/","title":{"rendered":"Ascending and Descending"},"content":{"rendered":"<h2>Lexicographic Ordering<\/h2>\n<p>Lexicographic ordering is what the APL primitives <code class=\"language-apl\">\u234b<\/code> and <code class=\"language-apl\">\u2352<\/code> provide:<\/p>\n<pre><code class=\"language-apl\">   \u2395io\u21900     \u235d \u2395io <em>delenda est<\/em>\r\n   \u2395rl\u21907*5   \u235d to get reproducible random results\r\n\r\n   a\u2190?11 3\u23743\r\n\r\n   a          a \u2337\u2368\u2282 \u234ba\r\n2 1 0      0 1 0\r\n0 2 2      0 2 2\r\n1 1 1      1 0 0\r\n1 0 0      1 0 1\r\n1 1 1      1 0 1\r\n1 2 1      1 1 0\r\n1 0 1      1 1 1\r\n1 0 1      1 1 1\r\n1 1 0      1 2 1\r\n0 1 0      1 2 2\r\n1 2 2      2 1 0\r\n<\/code><\/pre>\n<p>First order by column 0, resulting in groups of rows with the same values in column 0. Then, within each group, order by column 1, getting subgroups with the same values in columns 0 and 1. Then, within each subgroup, order by column 2, getting subsubgroups with the same values in columns 0, 1, and 2. In general, for each subsub\u2026subgroup, order by column <code class=\"language-apl\">k<\/code>, getting groups with identical values in columns <code class=\"language-apl\">\u2373k<\/code>.<\/p>\n<p>The preceding discourse is descriptive rather than prescriptive\u2014algorithms for <code class=\"language-apl\">\u234b<\/code> can use more efficient and more straightforward approaches. As well, for ease of understanding, the description is for a matrix and speaks of columns and rows. In general, any non-scalar array can be ordered, whence instead of rows, think major cells, instead of column, think item in a major cell. Symbolically and more succinctly, <code class=\"language-apl\">\u234b\u2375 \u2190\u2192 \u234b\u236a\u2375<\/code>.<\/p>\n<p><code class=\"language-apl\">\u234b<\/code> can be used if the orderings in the process are all ascending, or <code class=\"language-apl\">\u2352<\/code> if all descending. The problem to be solved here is where the orderings are a mix of ascending and descending. <\/p>\n<h2>Numeric Arrays<\/h2>\n<p>Since <code class=\"language-apl\">\u2352\u2375 \u2190\u2192 \u234b-\u2375<\/code> if <code class=\"language-apl\">\u2375<\/code> is numeric, for such <code class=\"language-apl\">\u2375<\/code> multiply each descending column by <code class=\"language-apl\">\u00af1<\/code> and each ascending column by <code class=\"language-apl\">1<\/code>, then apply <code class=\"language-apl\">\u234b<\/code>. This induces a \u201ccontrol array\u201d having the same shape as a major cell of the argument, with a <code class=\"language-apl\">\u00af1<\/code> for descending and a <code class=\"language-apl\">1<\/code> for ascending. <\/p>\n<pre><code class=\"language-apl\">   adn\u2190{\u2375 \u2337\u2368\u2282 \u234b \u237a \u00d7\u236499 \u00af1 \u22a2\u2375}\r\n<\/code><\/pre>\n<p>For the array <code class=\"language-apl\">a<\/code> in the previous section:<\/p>\n<pre><code class=\"language-apl\">   a              1 \u00af1 1 adn a        \u00af1 1 1 adn a\r\n2 1 0          0 2 2               2 1 0  \r\n0 2 2          0 1 0               1 0 0  \r\n1 1 1          1 2 1               1 0 1  \r\n1 0 0          1 2 2               1 0 1  \r\n1 1 1          1 1 0               1 1 0  \r\n1 2 1          1 1 1               1 1 1  \r\n1 0 1          1 1 1               1 1 1  \r\n1 0 1          1 0 0               1 2 1  \r\n1 1 0          1 0 1               1 2 2  \r\n0 1 0          1 0 1               0 1 0  \r\n1 2 2          2 1 0               0 2 2  \r\n<\/code><\/pre>\n<p>In <code class=\"language-apl\">1 \u00af1 1 adn a<\/code>, column 0 is ascending, and within <em>that<\/em>, column 1 is descending, and within that, column 2 is ascending. In <code class=\"language-apl\">\u00af1 1 1 adn a<\/code>, column 0 is descending, and within that, column 1 is ascending, and within that, column 2 is ascending.<\/p>\n<h2>Ordinals<\/h2>\n<p>An array to be sorted can be converted to an order-equivalent integer array by assigning to each item an ordinal (an integer) which has the same ordering relationship as the original item relative to other items in the array:<\/p>\n<pre><code class=\"language-apl\">   sort    \u2190 {(\u2282\u234b\u2375)\u2337\u2375}\r\n   ordinal \u2190 {\u2395ct\u21900 \u22c4 \u2375\u2373\u2368sort,\u2375}\r\n<\/code><\/pre>\n<p>That is, the ordinals obtain as the indices of the original array in the sorted list of the ravelled elements, using exact comparisons. (Exact comparisons are used because sorting necessarily uses exact comparisons.)<br \/>\nFor example:<\/p>\n<pre><code class=\"language-apl\">   \u22a2 d\u2190\u00af1 'syzygy' (3 \u00af5) 1j2 'chthonic' (\u00af1)\r\n\u250c\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2510\r\n\u2502\u00af1\u2502syzygy\u25023 \u00af5\u25021J2\u2502chthonic\u2502\u00af1\u2502\r\n\u2514\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2518\r\n   ordinal d\r\n0 5 3 2 4 0\r\n<\/code><\/pre>\n<p>In the example, the data items are <code class=\"language-apl\">\u00af1<\/code>, <code class=\"language-apl\">'syzygy'<\/code>, <code class=\"language-apl\">'chthonic'<\/code>, <code class=\"language-apl\">3 \u00af5<\/code>, <code class=\"language-apl\">1j2<\/code>, and <code class=\"language-apl\">\u00af1<\/code> again. With respect to ordering, these data items are perfectly represented by the ordinals (numbers) 0, 5, 3, 2, 4, and 0, respectively. That is, <code class=\"language-apl\">\u234bd \u2190\u2192 \u234bordinal d<\/code>.<\/p>\n<pre><code class=\"language-apl\">   \u234b d\r\n0 5 3 2 4 1\r\n   \u234b ordinal d\r\n0 5 3 2 4 1\r\n<\/code><\/pre>\n<p>As the example illustrates, it is imperative that identical ordinals are assigned to identical items, else the ordering relationships would be changed. For example, if <code class=\"language-apl\">b\u21900,\u236a2 1<\/code> and the 0s are assigned different ordinals,<\/p>\n<pre><code class=\"language-apl\">   \u22a2 b\u21900,\u236a2 1               \r\n0 2\r\n0 1\r\n   ordinal b                 \u22a2 bo\u21900 3,\u236a1 2  \u235d faux ordinals\r\n0 3                       0 3\r\n0 2                       1 2\r\n   \u234b ordinal b               \u234b bo\r\n1 0                       0 1\r\n   \u234b b\r\n1 0\r\n<\/code><\/pre>\n<p>Computation of ordinals is greatly facilitated by the <a target=\"_blank\" href=\"http:\/\/www.jsoftware.com\/papers\/TAOaxioms.htm\">total array ordering<\/a> introduced in <a target=\"_blank\" href=\"http:\/\/www.dyalog.com\/uploads\/conference\/dyalog18\/workshops\/TP1_Dyalog_Version_17_In_Depth.pdf#page=109\">Dyalog APL version 17.0<\/a>.<\/p>\n<h2>Non-Numeric Arrays<\/h2>\n<p>A general solution for the ordering problem obtains by first converting the array to an order-equivalent integer array through the use of ordinals.<\/p>\n<pre><code class=\"language-apl\">   ado \u2190 {\u2375 \u2337\u2368\u2282 \u234b \u237a \u00d7\u236499 \u00af1 ordinal \u2375}\r\n<\/code><\/pre>\n<p>For example:<\/p>\n<pre><code class=\"language-apl\">   \u2395rl\u21907*5   \u235d to get reproducible random results\r\n\r\n   x0\u2190 ?19\u23744                            \r\n   x1\u2190 (\u2282?19\u23742) \u2337 'alpha' 'beta'          \r\n   x2\u2190 (\u2282?19\u23743) \u2337 'abc'                   \r\n   x3\u2190 (\u2282?19\u23743) \u2337 'able' 'baker' 'charlie'\r\n\r\n   x \u2190 x0,x1,x2,\u236ax3\r\n\r\n   ordinal x\r\n13 49 19 42\r\n10 49 32 68\r\n13 49 63 68\r\n 4 49 63 42\r\n 0 27 19 23\r\n13 49 32 42\r\n 0 49 19 42\r\n10 49 32 68\r\n10 27 32 23\r\n 4 49 32 68\r\n 4 49 32 68\r\n 4 27 32 23\r\n 4 49 32 68\r\n 0 49 63 68\r\n13 49 63 68\r\n 0 49 32 42\r\n13 27 32 23\r\n 4 27 63 42\r\n13 49 19 42\r\n\r\n   (\u234bx) \u2261 \u234b ordinal x\r\n1\r\n<\/code><\/pre>\n<p>Suppose <code class=\"language-apl\">x<\/code> is to be sorted ascending in columns 0 and 2 and descending in columns 1 and 3. The control array is <code class=\"language-apl\">1 \u00af1 1 \u00af1<\/code>, and: <\/p>\n<pre><code class=\"language-apl\">   x                       1 \u00af1 1 \u00af1 ado x\r\n\u250c\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510     \u250c\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\r\n\u25022\u2502beta \u2502b\u2502baker  \u2502     \u25020\u2502beta \u2502a\u2502able   \u2502\r\n\u251c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524     \u251c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524\r\n\u25023\u2502alpha\u2502a\u2502able   \u2502     \u25020\u2502beta \u2502b\u2502charlie\u2502\r\n\u251c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524     \u251c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524\r\n\u25023\u2502beta \u2502b\u2502able   \u2502     \u25020\u2502beta \u2502b\u2502baker  \u2502\r\n\u251c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524     \u251c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524\r\n\u25023\u2502alpha\u2502b\u2502baker  \u2502     \u25020\u2502beta \u2502c\u2502charlie\u2502\r\n\u251c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524     \u251c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524\r\n\u25021\u2502beta \u2502b\u2502charlie\u2502     \u25020\u2502beta \u2502c\u2502able   \u2502\r\n\u251c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524     \u251c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524\r\n\u25021\u2502beta \u2502a\u2502baker  \u2502     \u25020\u2502alpha\u2502c\u2502baker  \u2502\r\n\u251c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524     \u251c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524\r\n\u25020\u2502beta \u2502c\u2502charlie\u2502     \u25021\u2502beta \u2502a\u2502baker  \u2502\r\n\u251c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524     \u251c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524\r\n\u25020\u2502beta \u2502b\u2502baker  \u2502     \u25021\u2502beta \u2502b\u2502charlie\u2502\r\n\u251c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524     \u251c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524\r\n\u25020\u2502beta \u2502c\u2502able   \u2502     \u25021\u2502alpha\u2502c\u2502able   \u2502\r\n\u251c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524     \u251c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524\r\n\u25020\u2502beta \u2502a\u2502able   \u2502     \u25022\u2502beta \u2502a\u2502baker  \u2502\r\n\u251c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524     \u251c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524\r\n\u25023\u2502alpha\u2502a\u2502baker  \u2502     \u25022\u2502beta \u2502b\u2502baker  \u2502\r\n\u251c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524     \u251c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524\r\n\u25023\u2502alpha\u2502a\u2502baker  \u2502     \u25023\u2502beta \u2502b\u2502able   \u2502\r\n\u251c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524     \u251c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524\r\n\u25021\u2502alpha\u2502c\u2502able   \u2502     \u25023\u2502beta \u2502b\u2502able   \u2502\r\n\u251c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524     \u251c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524\r\n\u25020\u2502beta \u2502b\u2502charlie\u2502     \u25023\u2502beta \u2502c\u2502charlie\u2502\r\n\u251c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524     \u251c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524\r\n\u25020\u2502alpha\u2502c\u2502baker  \u2502     \u25023\u2502alpha\u2502a\u2502baker  \u2502\r\n\u251c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524     \u251c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524\r\n\u25023\u2502beta \u2502b\u2502able   \u2502     \u25023\u2502alpha\u2502a\u2502baker  \u2502\r\n\u251c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524     \u251c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524\r\n\u25022\u2502beta \u2502a\u2502baker  \u2502     \u25023\u2502alpha\u2502a\u2502able   \u2502\r\n\u251c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524     \u251c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524\r\n\u25023\u2502beta \u2502c\u2502charlie\u2502     \u25023\u2502alpha\u2502a\u2502able   \u2502\r\n\u251c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524     \u251c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524\r\n\u25023\u2502alpha\u2502a\u2502able   \u2502     \u25023\u2502alpha\u2502b\u2502baker  \u2502\r\n\u2514\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518     \u2514\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\r\n<\/code><\/pre>\n<h2>Finally<\/h2>\n<pre><code class=\"language-apl\">   (ordinal x) \u2261 ordinal ordinal x\r\n1\r\n<\/code><\/pre>\n<p>That is, <code class=\"language-apl\">ordinal<\/code> is idempotent. Actually, this is kind of obvious, but I never miss an opportunity to use the word \u201cidempotent\u201d.\u263a <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Lexicographic Ordering Lexicographic ordering is what the APL primitives \u234b and \u2352 provide: \u2395io\u21900 \u235d \u2395io delenda est \u2395rl\u21907*5 \u235d to get reproducible random results a\u2190?11 3\u23743 a a \u2337\u2368\u2282 \u234ba 2 1 0 0 1 0 0 2 2 &hellip; <a href=\"https:\/\/www.dyalog.com\/blog\/2019\/02\/ascending-and-descending\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13,8],"tags":[],"class_list":["post-5706","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-apl-language"],"_links":{"self":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/5706","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/comments?post=5706"}],"version-history":[{"count":32,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/5706\/revisions"}],"predecessor-version":[{"id":5804,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/5706\/revisions\/5804"}],"wp:attachment":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/media?parent=5706"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/categories?post=5706"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/tags?post=5706"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}