Topic: `APLX Help` : `Help on APL language` : `APL Primitives` : `⍒ Grade down`
[ Previous | Next | Contents | Index | APL Home ]

# `⍒` Grade down

Grade down enables numbers or characters to be sorted into descending order. The arguments to grade down must be simple and not mixed. The right argument is a simple numeric or character array containing the data you want to sort. A left argument may be used to specify a collation sequence for character arrays.

The result is a vector which identifies elements by their position in the original data. For matrices or higher dimensional arrays, the sort is carried out on the first dimension. The result of grade down can then be used to index the right argument into descending order. `⍒` is affected by `⎕IO,` the index origin.

Identical elements or subarrays within the right argument will have the same relative positions in the result.

## One-argument form

With the one-argument form, a numeric argument is sorted into descending order. With a character argument `⎕AV` (the 'atomic vector') determines sorting order. It puts numeric characters before alphabetic characters and uses normal alphabetic order. So '1' is before (or less than) 'A', and 'A' is before 'Z'.

```             ⍒13 8 122 4             (Produces vector showing ranking: 3rd
3 1 2 4                        number is biggest, 1st is next etc)
(13 8 122 4)[3 1 2 4]   (Ranking order used as index to put
122 13 8 4                     numbers in descending order)
⍒'ABRACADABRA'          (Produces vector showing ranking: 'R'
3 10 7 5 2 9 1 4 6 8 11        in position 3 is 'biggest', etc)
KEY←⍒'ABRACADABRA'      (The ranking vector is put in KEY
'ABRACADABRA'[KEY]       and is used as an index to put the
RRDCBBAAAAA                    original data into descending order)
TABLE                   (A 3-row 3-column matrix)
BOB
ALF
ZAK
⍒ TABLE                 (Ranks the names in descending
3 1 2                          alphabetic order)
TAB
4 5 6                         (Sorts TAB by row)
1 1 3
1 1 2
⍒TAB
1 2 3
TAB[⍒TAB;]              (TAB in descending order)
4 5 6
1 1 3
1 1 2
ARRAY                   (Three dimensional array is sorted by the
2  3  4                       first dimensions, the planes)
0  1  0
```
```        1  1  2
10 11 12
```
```        1  1  3
4  5  6
ARRAY[⍒ARRAY;;]         (ARRAY in descending order, by planes)
2  3  4
0  1  0
```
```        1  1  3
4  5  6
```
```        1  1  2
10 11 12
```
```             NAMES                    (Three dimensional character array)
JOE
DOE
```
```       BOB
JONES
```
```       BOB
ZWART
⍒NAMES
1 3 2
NAMES[⍒NAMES;;]
JOE
DOE
```
```       BOB
ZWART
```
```       BOB
JONES
```

## Two-argument form

The two argument form can only be used with simple character arrays. The left argument specifies the collation order you want to use.

```             'ZYXWVUTSRQPONMLKJIHGFEDCBA' ⍒'ABRACADABRA'
1 4 6 8 11 2 9 5 7 3 10       (Collation order reversed. Compare
results with the example above)
```

The system variable `⎕A,` containing the alphabet, and the function `⌽,` are used to reverse the alphabet in the next example.

```             TABLE
BOB
ALF
ZAK
(⌽⎕A)⍒TABLE
2 1 3                         (Compare with the example above)
```

When the left argument is a character matrix (or higher dimension array), more sophisticated sorts can be devised. When elements of the right argument are found in the left argument they are assigned a priority depending on their position in the collation array. For this purpose, the last axis of the collating array is deemed to have most significance, and the first the least significance.

If elements in the right argument are not present in the collating array, they given priorities as if they were found at the end of the collating array and in the order of their occurrence in the unsorted right argument.

A common use of a matrix collation sequence is to carry out a case-insensitive sort. In the following example, lower case characters are used in the array to be sorted. (Some implementations of APLX will use underlined letters instead of lowercase letters).

```             DATA
ABLE
aBLE
ACRE
ABEL
aBEL
ACES
COLL
ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz
COLL⍒DATA
3 6 2 1 5 4
DATA[COLL⍒DATA;]
ACRE
ACES
aBLE
ABLE
aBEL
ABEL
```

The collation array COLL places lower case characters in the second row of the collation matrix. When the variable DATA is sorted, the first sort is by column order in the collation array. Thus rows in the matrix being sorted beginning with the letter 'A' or 'a' will be given highest priority, followed by 'B' or 'b' and so on for successive columns within the array being sorted. The next sort is by the rows of the collation matrix, and 'A' is given a higher priority than 'a' and so on. Contrast the example above, with a similar sort using a one dimensional (vector) collation sequence:

```             COLL1
Topic: `APLX Help` : `Help on APL language` : `APL Primitives` : `⍒ Grade down`