Sorts units of vectors according to an index vector.
Options
INDEX = vectors |
Variates, texts or factors whose values are to define the ordering; default is to use the first vector in the OLDVECTOR list |
|---|---|
DIRECTION = string token |
Order in which to sort (ascending, descending); default asce |
DECIMALS = scalar |
Number of decimal places to which to round before sorting numbers; default * i.e. no rounding |
Parameters
OLDVECTOR = vectors or pointers |
Factors, pointers, texts or variates whose values are to be sorted |
|---|---|
NEWVECTOR = vectors or pointers |
Structure to receive each set of sorted values; if any are omitted, the values are placed in the corresponding OLDVECTOR |
Description
The SORT directive allows you to reorder the units of a list of vectors or pointers according to one or more “index” vectors. These can be specified explicitly using the INDEX option (and they need not be among the vectors actually sorted). If you omit the INDEX option, Genstat uses the first vector in the OLDVECTOR list. The DECIMALS option allows you to define the number of decimal places that are taken into account for an index variate: for example DECIMALS=0 would round each value to the nearest integer. If you do not set this, there is no rounding. The DIRECTION option controls whether the ordering is into ascending or descending order; by default DIRECTION=ascending.
The vectors or pointers whose values are to be sorted are listed by the OLDVECTOR parameter. The units of each structure are permuted in exactly the same way, into an ordering determined from the index vectors. The NEWVECTOR parameter allows you to specify new vectors to contain the sorted values, and thus keep the unsorted values in the original vectors. For example
SORT [INDEX=Name] Age,Income,Name,Sex; NEWVECTOR=A,*,N,S
would place the sorted values of Age, Name and Sex into A, N and S; as there is a null entry (*) corresponding to Income in the NEWVECTOR list, the sorted incomes would replace the original values of Income. Any undeclared vector in the NEWVECTOR list is declared implicitly to match the corresponding OLDVECTOR.
Options: INDEX, DIRECTION, DECIMALS.
Parameters: OLDVECTOR, NEWVECTOR.
Action with RESTRICT
You can restrict the index vector, or any of the oldvectors, to sort only a subset of the units. Each of the units that is not in the subset is left in its original position.
See also
Directive: CALCULATE.
Function: SORT
Commands for: Calculations and manipulation.
Example
" Example SORT-1: Use of the SORT directive" VARIATE [VALUES=21,50,24,49,29,42,32,42,36,40] A & [VALUES=3000,17500,5000,20000,7000,4500,12000,18000,15500,17500] I TEXT [VALUES=Clarke,Irving,Adams,Jones,Day,Good,Edwards,Baker,Hall,Field] N FACTOR [LABELS=!T(male,female); VALUES=2,1,1,1,2,2,1,1,2,1] S " sort A, I, N & S according to alphabetical order for N, storing sorted values in Age, Income, Name & Sex " SORT [INDEX=N] OLDVECTOR=A,I,N,S; NEWVECTOR = Age,Income,Name,Sex PRINT Name,Sex,Age,Income " sort A, I, N & S according to ascending values of A, storing sorted values in Age, Income, Name & Sex " SORT [INDEX=A] OLDVECTOR=A,I,N,S; NEWVECTOR=Age,Income,Name,Sex PRINT Name,Sex,Age,Income " sort A, I, N & S according to descending values of I, storing sorted values in Age, Income, Name & Sex " SORT [INDEX=I; DIRECTION=descending] OLDVECTOR=A,I,N,S;\ NEWVECTOR=Age,Income,Name,Sex PRINT Name,Sex,Age,Income