## “ Grade up

Grade up enables numbers or characters to be sorted into ascending order. The arguments to grade up 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 sort 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 up can be used to index the right argument into ascending 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 ascending 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:
           4 2 1 3                        4th number is first, 2nd number next)
                 (13 8 122 4)[4 2 1 3]   (Ranking order is used as an index
           4 8 13 122                     to put numbers in ascending order)
                 “'ZAMBIA'               (Produces vector showing ranking.
           2 6 4 5 3 1                    'A' in position 2 is first)
                 'ZAMBIA'[“'ZAMBIA']     (The ranking order found in the []'s
           AABIMZ                         is used as an index to
                                          put the characters in order)
                 TABLE                   (A 3-row 3-column matrix of names)
           BOB
           ALF
           ZAK
                 “TABLE                  (Ranks the names in alpha order)
           2 1 3                         (By row)
                 TAB
           4 5 6                         (Sorts TAB by row)
           1 1 3
           1 1 2
                 “TAB
           3 2 1
                 TAB[“TAB;]              (TAB in ascending order)
           1 1 2
           1 1 3
           4 5 6
                 ARRAY                   (Three dimensional array is sorted by the
            2  3  4                       first dimensions, the planes)
            0  1  0

            1  1  3
            4  5  6

            1  1  2
           10 11 12
                 ARRAY[“ARRAY;;]         (ARRAY in ascending order by planes)
            1  1  2
           10 11 12

            1  1  3
            4  5  6

            2  3  4
            0  1  0
                 NAMES                    (Three dimensional character array)
           JOE
           DOE

           BOB
           JONES

           BOB
           ZWART
                 “NAMES
           2 3 1
                 NAMES[“NAMES;;]
           BOB
           JONES

           BOB
           ZWART

           JOE
           DOE

### 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' “ 'ZAMBIA'
            1 3 5 4 2 6                   (Collation order reversed. Compare
                                          result 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
           3 1 2                         (Compare with 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, lowercase 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
           4 5 1 2 6 3
                 DATA[COLL“DATA;]
           ABEL
           aBEL
           ABLE
           aBLE
           ACES
           ACRE

The collation array COLL contains lowercase characters in its second row. 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
            AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz
                 DATA[COLL1“DATA;]
           ABEL
           ABLE
           ACES
           ACRE
           aBEL
           aBLE

Here, all rows beginning with 'A' are given a higher priority to rows beginning with 'a'.

Topic: APLX Help : Help on APL language : APL Primitives : “ Grade up

[ Previous | Next | Contents | Index ]