# SORT

Compatible with:
DOS Maximite CMM MM150 MM170 MM+ MMX Picromite ArmiteL4 Armite F4 ArmiteH7 Picomite CMM2

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

Description:
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.
flag values are:
bit0: 0 (default if omitted) normal sort - 1 reverse sort
bit1: 0 (default) case dependent - 1 sort is case independent
startposition defines which element in the array to start the sort. Default is 0 (OPTION BASE 0) or 1 (OPTION BASE 1)
elementstosort defines how many elements in the array should be sorted. Default is all elements after the startposition

After the sort ‘indexarray%()’ 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 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

OPTION EXPLICIT
OPTION DEFAULT NONE
DIM INTEGER maxFiles = 500 ' adjust to suit expected maximum
DIM fList$(maxFiles) LENGTH 80' file names for sorting
DIM fSize%(maxFiles) ' file sizes
DIM fDate$(maxFiles) LENGTH 20' file dates
DIM fSort$(maxFiles) LENGTH 80
DIM fsortSize%(maxFiles)
DIM fIndex%(maxFiles)
DIM INTEGER mf, z
DIM f$
' by ignoring array location zero, we can work safely with OPTION BASE 0 or 1
'
mf = 1
f$ = DIR$("*", ALL)
DO WHILE f$ <> ""
fSize%(mf) = MM.INFO$(FILESIZE f$)
IF fSize%(mf)=-2 THEN
fList$(mf) = "(DIR) "+f$
fDate$(mf) = " "+MM.INFO$(MODIFIED f$)' the leading space keeps directories sorted first
ELSE
fList$(mf) = f$
fDate$(mf) = "."+MM.INFO$(MODIFIED f$)
ENDIF
mf = mf + 1
IF mf > maxFiles THEN EXIT DO
f$ = DIR$()
LOOP
mf = mf -1 ' we now know the number of entries
' print the unsorted list
PRINT
PRINT "Unsorted lsit:"
FOR z = 1 TO mf
PRINT fList$(z)
NEXT z
FOR z = 1 TO mf
fSort$(z)= fList$(z)
NEXT z
SORT fSort$(), fIndex%(),&b10,1, mf
' print the sorted list using the index array to refer to the original data
PRINT
PRINT "Sorted by Name with directories first"
FOR z = 1 TO mf
PRINT STR$(fIndex%(z),4);" ";fList$(fIndex%(z))
NEXT z
' sort by date
FOR z = 1 TO mf
fSort$(z)= fDate$(z)
NEXT z
SORT fSort$(), fIndex%(),&b10,1, mf
' print the sorted list
PRINT
PRINT "Sorted by Date with directories first"
FOR z = 1 TO mf
PRINT STR$(fIndex%(z),4);" "; fDate$(fIndex%(z));" Â ";STR$(fSize%(fIndex%(z)),10,0);" Â "; fList$(fIndex%(z))
NEXT z
' sort by file size using numbers instead of strings
MATH add fSize%(), 0, fSortSize%()
SORT fSortSize%(), fIndex%(),&b10,1, mf
' print the sorted list
PRINT
PRINT "Sorted by Size with directories first because size = -2"
' this time we print index zero to show it is not hiding any data.
FOR z = 0 TO mf
PRINT STR$(fIndex%(z),4);" "; STR$(fSize%(fIndex%(z)),10,0);" Â ";fDate$(fIndex%(z));" Â "; fList$(fIndex%(z))
NEXT z

Last edited: 25 February, 2021