I have the following code that should return me an sorted array on the basis of its 'pos' value.
local tables = {}
table.insert(tables,{ ['pos']=2, ['name'] = 'C' })
table.insert(tables, {['pos']=1, ['name'] = 'A' })
table.insert(tables,{ ['pos']=30, ['name'] = 'D'} )
function comp(w1,w2)
if tonumber(w1['pos']) > tonumber(w2['pos']) then
return true
end
end
table.sort(tables, comp)
for key,val in pairs(tables) do
print(val['name'])
end
Result is:
D C A
Expected (alphabetically sorted by its "pos"):
A, C, D
Whats wrong?