.wip

SORT array() [,indexarray()] [,flags] [,startposition] [,elementstosort]

This command takes an array of any type (integer, float or string) and sorts it into ascending order in place.

It has an optional parameter ‘indexarray%()’. If used this must be an integer array of the same size as the array to be sorted.

After the sort this array will contain the original index position of each element in the array being sorted before it was sorted. Any data in the array will be overwritten. This allows connected arrays to be sorted.

The flag parameter is optional and valid flag values are: bit0: 0 (default if omitted) normal sort - 1 reverse sort bit1: 0 (default) case dependent - 1 sort is case independent (strings only). bit2: 0 (default) normal sort - 1 empty strings go to the end of the array

The optional startposition defines which element in the array to start the sort. Default is 0 (OPTION BASE 0) or 1 (OPTION BASE 1) The optional elementstosort defines how many elements in the array should be sorted. The default is all elements after the startposition. Any of the optional parameters may be omitted so, for example, to sort just the first 50 elements of an array you could use: SORT array(), , , ,50 Example: The array city$() might contain the names of world cities and can be easily sorted into increasing alphabetical order with the command: SORT city$() The SORT command will work with strings, floats and integers however the array to be sorted must be single dimensioned. Often data is held in multiple arrays, for example, the name of each city might be held in the array city$(), the population held in the array pop%() and the size of the city held in area!(). The same index would refer to the name, population and the area of the city. Sorting and accessing this data is a little more complex but it can be done relatively easily using an optional parameter to the sort command as follows: SORT array(), indexarray%() indexarray%() must be a single dimension integer array of the same size as the array being sorted. Following the sort indexarray%() will contain the corresponding index to the original data before it was sorted. (anything previously in indexarray%() will be overwritten). To access the sorted data you would first copy the array holding the main key to a temporary array and sort that while specifying indexarray%(). After the sort indexarray%() can be used to index the original arrays. For example:

DIM city$(100),pop%(100),area!(100),sindex%(100),t$(100)
FOR i = 0 to 100
  t$(i) = city$(i)            ‘ temporary copy of the keys
NEXT i
SORT t$(), sindex%()            ‘ sort the temporary array
FOR i = 0 to 100
  k = sindex%(i)             ‘ index to the original array
  PRINT city$(k),pop%(k),area!(k)  ‘ print in sorted order
NEXT i