I have two sets of variables
time_n (where n is a number assigned at time of dynamic creation)
name_n (where n is a number assigned at time of dynamic creation)
time_1 is related to name_1 in that they are referring to the same item the user has inputted
What I have been trying to do is sort though the times, to list the times in numerical order with the related name as well.
When I sort I need to keep the key reference of the times the same, so I can then refer to the name. I have tried putting my key,value pair into another table, but I’m getting very lost.
Can anyone suggest some methods for achieving this please.
Thanks
--# Main
-- test sort
function setup()
t={ {name="a", time=1},{name="b", time=10},{name="c", time=5} }
print("before sorting")
for i,v in ipairs(t) do print( v.name .." " ..tostring(v.time)) end
comp = function(a,b) return (a.time>b.time) end
table.sort(t,comp)
print("after sorting")
for i,v in ipairs(t) do print( v.name .." " ..tostring(v.time)) end
end
avoid the _1, _2 etc, just make table elements that are small tables as above
tab={}
tab[1]={time=10,name="cascade"}
tab[2]={time=5,name="Centennial"}
tab[3]={time=45,name="Pacifica"}
table.sort(tab,
function(a,b)
return (a.time>b.time)
end)
for k,v in ipairs(tab) do
print(v.name.." "..v.time)
end