Sorting Arrays using the VAR Command
- Last UpdatedJan 08, 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 colour, and then on year, you 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 CIASCII !Colour !Year NUMERIC !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 CIASCII !Colour !Year NUMERIC 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.