I've developed this code:
function Sort(ar, num)
-- sorts a 0-indexed array into order, and returns the sorted array
-- inputs are (array to be sorted, number of elements to sort)
-- typically inputs will be like: myarray = Sort(myarray, # myarray)
-- Lua is stupid and sorts tables from 1-to-arraylength instead of indexing from 0 like any sensible language
-- so we must copy the incoming array to a temporary renumbered array.
temp = {}
for i = 1,num+1 do
temp[i] = ar[i-1]
end
-- copied to temporary array
-- now sort
table.sort(temp)
-- sorted. now copy back to real array
for i = 1,num+1 do
ar[i-1] = temp[i]
end
return ar
end
Also tonight I learned a useful feature of lua. If you write something like:
x = # myarray
then x is now equal to the length of the array!
So for example, you can do:
for ID = 0, # myarray
myarray[ID] = nil
end
That would find all existing entries in the array and set them to nil.
# <array> returns the length of the array.
If you have elements [0 ], [1], and [2], using # array will return 2. If you just have elements [1] and [2] then it will still return 2. It seems to start counting from 1..