Hey!
I have a function that should sort Table. For some reason table.sort
tries to sort the same element/key in table.
Heres my script i made to test it out:
Script/Code
local ItemsData = {
["Bloxy Cola"] = {}--some data here
}
local PreOrder = {}
local DATA = {
Team1 = {{"NPC1"}, {"NPC2"}, {"NPC3"}, {"NPC4"}},
Moves = {
["NPC1"] = {"Punch"},
["NPC2"] = {"Punch"},
["NPC3"] = {"Bloxy Cola"},
["NPC4"] = {"Punch"}
}
}
for i=1, #DATA .Team1 do
table.insert(PreOrder , {DATA.Team1[i][1], DATA.Moves[DATA .Team1[i][1]]})
end
local function getNumber(Dat, a)
for k=1, #Dat.Team1 do
local v = Dat.Team1[k]
if v[1] == false then
if v[2] == a[1] then
return k
end
else
if v[1] == a[1] then
return k
end
end
end
end
table.sort(PreOrder,
function(a,b)
if ItemsData[a[2][1]] and not ItemsData[b[2][1]] then
return true
elseif ItemsData[b[2][1]] and not ItemsData[a[2][1]] then
return false
end
local NumA = getNumber(DATA, a)
local NumB = getNumber(DATA, b)
if NumA < NumB then
return true
elseif NumA > NumB then
return false
end
print("Index - {"..tostring(NumA)..", "..tostring(NumB).."} "..
"Names: - {"..tostring(a[1])..", "..tostring(b[1]).."}"
)
end
)
What my output says:
Is there any explanation why this happen? How can i fix this? Because of this, sometimes, it will sort my table wrongly…