Arrays can be sorted using the sort command and searched using the find command.
This program shows the sort command being used. It will organise the elements of an array into ascending order.
speed as integer [ 5 ] = [ 0, 500, 300, 100, 400, 200 ]
speed.sort ( )
do for i = 1 to speed.length print ( speed [ i ] ) next i
sync ( ) loop
Prior to the sort command being used the array contains the values 500, 300, 100, 400 and 200. Once it has been sorted it will change to 100, 200, 300, 400 and 500.
The sort command can sort arrays of floats, integers and strings. The next program uses it to sort an array of names.
names as string [ 5 ] = [ "", "lee", "rick", "paul", "dave", "adam" ]
names.sort ( )
do for i = 1 to names.length print ( names [ i ] ) next i
sync ( ) loop
The find commands provides a way of locating specific information within the array. Say that you wanted to find which index the name "lee" is stored in.
names as string [ 5 ] = [ "", "lee", "rick", "paul", "dave", "adam" ]
names.sort ( )
do for i = 1 to names.length print ( names [ i ] ) next i
print ( "" )
index = names.find ( "lee" )
print ( "lee is at index = " + str ( index ) )
sync ( ) loop
The array gets declared, sorted and then inside the main loop has its contents displayed on screen. The find command is called on the array, passing in the name "lee". The return value lets us know where "lee" is in the array.
If you attempt to search for an integer, string or floating point value that is not contained within the array then the command will return -1.