for i,v in pairs(mytable)do
if type(v) ~= "string" then
table.remove(mytable,i)
end
end
table.sort(mytable)
Error: attempt to compare string with userdata
Someone please explain -_-?
for i,v in pairs(mytable)do
if type(v) ~= "string" then
table.remove(mytable,i)
end
end
table.sort(mytable)
Error: attempt to compare string with userdata
Someone please explain -_-?
You're shifting the table, but iterating forwards. This has the awkward side-effect of causing the iteration to skip some keys.
The solution? Iterate backwards through the table.
for i=#mytable, 1, -1 do
local v = mytable[i];
if type(v) ~= "string" then
table.remove(mytable,i)
end
end
table.sort(mytable)
This will avoid skipping iterations, and it will mean that the internal comparison from table.sort won't whine about comparing anything other than strings.
You havent put anything in the table
This discussion is preserved as it was. New answers, comments, and votes are closed.
4 comments
First define the table
If there wasn't anything in the table it wouldn't tell me it attempted to compare something with something if there was nothing.
v is the things in the table but theres nothing in v and u trying to compare to something
try this: myTable = [1, 2 , 4]