Apart from arrays and RNG's (Random Number Generators), all the changes here are pretty self-explanatory.
Here's a piece of code that demonstrates sorting - for best results, set as windowed, 640x640 (yes, you'll be looking through square window!):
set window on
if screen width() <> 640 or screen height() <> 640
set display mode 640, 640, screen depth()
endif
sync sleep 1
sync off
randomize 1 ` Make sure we are getting the same numbers each time
#constant ROWCOUNT = 20
` Here's the initial type that we will base our array on
type MyData_t
Sequence as integer
Txt as string
Rand as integer
endtype
` Here's the format string definition for the above type - 0 is integer, 2 is string
` See the help for SET ARRAY FORMAT for a full list of the types
global MyData_format as string = "020"
` Define the array, and set its format
dim MyData(ROWCOUNT) as MyData_t
set array format MyData(), MyData_format
` Not going to sort on this array, so no format is required.
dim Previous(ROWCOUNT) as MyData_t
` Set up the array with an initial set of values
for i = 0 to ROWCOUNT
MyData(i).Sequence = i
MyData(i).Txt = chr$( rnd(25) + 65 )
MyData(i).Rand = rnd(999)
Previous(i) = MyData(i)
next
DisplayArrays("Initial array values")
` **** SORT ON FIELD 2 ****
PrettyPrint("First, we'll simply sort the array by the Txt field.", "", "")
print ""
PrettyPrint("You do this using the sort command, specifying the array, and then following that with", _
"the number of the field you want to sort by.", "")
print ""
print " ==> sort array MyData(), 2"
print ""
print "Press a key to sort by the Txt field";
wait key
sort array MyData(), 2
DisplayArrays("Command: sort array MyData(), 2")
` **** SORT ON FIELD 2 AND 3 ****
PrettyPrint("Note that the current array contents have the Txt field sorted, but that the", _
"Rand column is randomly ordered where the Txt fields match.", _
"You can see this in the first two rows of data.")
print ""
PrettyPrint("Lets get the array sorted by both columns, with Txt taking the major part.", _
"You do this by specifying both field numbers to sort - in this case, fields 2 and 3.", _
"You can specify up to 6 fields for sorting.")
print ""
print " ==> sort array MyData(), 2, 3"
print ""
print "Press a key to sort by Txt and Rand";
wait key
sort array MyData(), 2, 3
DisplayArrays("Command: sort array MyData(), 2, 3")
` **** REVERSE SORT ON FIELD 2, FORWARD SORT ON FIELD 3 ****
PrettyPrint("Now we'll re-sort the data in reverse order of the Txt field, but keep the", _
"forward order of the Rand column.", "")
print ""
PrettyPrint("You do this by specifying the Txt field number as a negative value.", "", "")
print ""
print " ==> sort array MyData(), -2, 3"
print ""
print "Press a key to reverse sort Txt and forward sort Rand";
wait key
sort array MyData(), -2, 3
DisplayArrays("Command: sort array MyData(), -2, 3")
` **** FIELD 0 DOES NOTHING ****
PrettyPrint("The final thing to know is about field 0. It's a special field that is completely ignored by", _
"the sort command, i.e. it does nothing. When specifying the field numbers with variables, ", _
"you can set variables to 0 to vary the fields you sort by.")
print ""
print " ==> sort array MyData(), -2, 0, 0, 3"
print ""
print "Press a key to reverse sort Txt and forward sort Rand";
wait key
sort array MyData(), -2, 0, 0, 3
DisplayArrays("Command: sort array MyData(), -2, 0, 0, 3")
PrettyPrint("See? Nothing changed despite adding those two extra sort fields", "", "")
print ""
print "Press a key to end the example"
wait key
end
function DisplayArrays(Msg as string)
local S as string
cls
print Msg
print ""
print " |<---- Previous --->| |<---- Current ---->|"
print " Seq Txt Rand Seq Txt Rand"
for i = 0 to ROWCOUNT
S = padleft$( str$( i ), 2 )
S = S + " " + padleft$( str$( Previous(i).Sequence ), 4 )
S = S + " " + padright$( Previous(i).Txt, 11 )
S = S + " " + padleft$( str$( Previous(i).Rand ), 5 )
S = S + " " + padleft$( str$( MyData(i).Sequence ), 4 )
S = S + " " + padright$( MyData(i).Txt, 11 )
S = S + " " + padleft$( str$( MyData(i).Rand ), 5 )
print S
Previous(i) = MyData(i)
next
print ""
endfunction
function PrettyPrint(Msg1 as string, Msg2 as string, Msg3 as string)
local Msg as string
local P as integer
Msg = trim$(Msg1) + " " + trim$(Msg2) + " " + trim$(Msg3)
while len(Msg) > 80
for i = 81 to 1 step -1
if mid$( Msg, i, 1 ) = " "
print left$(Msg, i-1)
Msg = trimleft$( mid$(Msg, i+1, 0) )
exit
endif
if instr(",.!?:;", mid$( Msg, i, 1 )) > 0
print left$(Msg, i)
Msg = trimleft$( mid$(Msg, i+1, 0) )
exit
endif
next
endwhile
if Msg <> "" then print Msg
endfunction