Sorting Arrays Using the VAR Command
- Last UpdatedOct 29, 2024
- 2 minute read
These facilities were designed to work with arrays of STRINGS.
Where a multi-level sort is required, it is still necessary to use the older facilities of the VAR command to perform the sort. Using a different example, look at the arrays !Car, !Colour and !Year:
|
!Car |
!Colour |
!Year |
|
|
[1] |
CHARIOT |
MUD |
26 |
|
[2] |
FORD |
RED |
1978 |
|
[3] |
VAUXHALL |
YELLOW |
1990 |
|
[4] |
FORD |
YELLOW |
1993 |
|
[5] |
FORD |
(Unset) |
1986 |
|
[6] |
(Unset) |
BLUE |
1993 |
|
[7] |
Ford |
GREEN |
(Unset) |
|
[8] |
(Unset) |
(Unset) |
(Unset) |
|
[9] |
vauxhall |
YELLOW |
1994 |
|
[10] |
FORD |
YELLOW |
1993 |
If you want to sort the arrays by car model, then on color, and then on year, they would give the command:
|
VAR |
!Index |
SORT |
!Car |
CIASCII |
!Colour |
!Year |
NUMERIC |
The sorted index values are put into the array !Index (for clarity, shown here with the rows of the original arrays they are pointing to).
|
!Index |
!Car |
!Colour |
!Year |
||
|
[1] |
1 |
ð |
CHARIOT |
MUD |
2 |
|
[2] |
7 |
ð |
Ford |
GREEN |
(Unset) |
|
[3] |
2 |
ð |
FORD |
RED |
1978 |
|
[4] |
4 |
ð |
FORD |
YELLOW |
1993 |
|
[5] |
10 |
ð |
FORD |
YELLOW |
1993 |
|
[6] |
5 |
ð |
FORD |
(Unset) |
1986 |
|
[7] |
3 |
ð |
VAUXHALL |
YELLOW |
1990 |
|
[8] |
9 |
ð |
vauxhall |
YELLOW |
1994 |
|
[9] |
6 |
ð |
(Unset) |
BLUE |
1993 |
|
[10] |
8 |
ð |
(Unset) |
(Unset) |
(Unset) |
By default values are sorted ascending order using the ASCII character-codes. Other options which may be supplied after each array name are:
|
Sorting Option |
Effect |
|
CIASCII |
Sorts in ascending case-independent alphabetic order. |
|
DESCENDING |
Sorts in alphabetic in reverse order. |
|
CIASCII DESCENDING |
Sorts in descending case-independent alphabetic order. |
|
NUMERIC |
Forces an ascending numerical sort on numbers held as strings. |
|
NUMERIC DESCENDING |
Forces a descending numerical sort on numbers held as strings. |
You can also modify the array to eliminate empty or repeated elements:
|
Option |
Effect |
|
UNIQUE |
Eliminates instances of duplicated data. For example: VAR !Index SORT UNIQUE !Car !Index would then have only 9 elements as the value in the original row 10 was a second reference to a 1993 yellow Ford which is discarded. |
|
NOUNSET NOEMPTY |
Eliminates rows that contain only UNSET values. VAR !Index SORT NOUNSET !Car Again !Index would have only 9 elements as the original row 8 is discarded. The option NOEMPTY discards rows with values that are all blanks. |
To sort these arrays and identify the last occurrence of each group of the same car type, use the LASTINGROUP option:
|
VAR |
!Index |
SORT |
!Car |
LASTINGROUP |
!Group |
This would create !Index by sorting the values in the array !Car, but would also would also create the array !Last:
|
!Index |
!Group |
!Car |
||
|
[1] |
1 |
1 |
ð |
CHARIOT |
|
[2] |
2 |
ð |
FORD |
|
|
[3] |
4 |
ð |
FORD |
|
|
[4] |
5 |
ð |
FORD |
|
|
[5] |
10 |
2 |
ð |
FORD |
|
[6] |
3 |
3 |
ð |
VAUXHALL |
|
[7] |
7 |
4 |
ð |
ford |
|
[8] |
9 |
5 |
ð |
vauxhall |
|
[9] |
6 |
ð |
(Unset) |
|
|
[10] |
8 |
6 |
ð |
(Unset) |
A similar option FIRSTINGROUP identifies the first row of each group.