Table.sort tries to sort same element?

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:
Screenshot_385

Is there any explanation why this happen? How can i fix this? Because of this, sometimes, it will sort my table wrongly…

What are you trying to accomplish?
I feel like thered be a much simpler way to do whatever youre doing

This table:

local DATA = {
	Team1 = {{"NPC1"}, {"NPC2"}, {"NPC3"}, {"NPC4"}},
	Moves = {
		["NPC1"] = {"Punch"},
		["NPC2"] = {"Punch"},
		["NPC3"] = {"Bloxy Cola"},
		["NPC4"] = {"Punch"}
	}
}

It contains data about members of the Team and their currently selected moves/skills
I have other table - PreOrder, which contains array with Team Member ID/Name and his Move.

I sort this table (PreOrder) to be like this:
if Selected move is Item (Found inside ItemsData Table) then person who used that will move firstly, if both elements are Items/are not Items then it will sort table by their order in DATA.Team1

Example:

--EXAMPLE 1:
local DATA = {
	Team1 = {{"NPC1"}, {"NPC2"}, {"NPC3"}, {"NPC4"}},
	Moves = {
		["NPC1"] = {"Punch"},
		["NPC2"] = {"Punch"},
		["NPC3"] = {"Bloxy Cola"},
		["NPC4"] = {"Punch"}
	}
}

--After i sorted table it should be like this:

{NPC3, NPC1, NPC2, NPC4}

--EXAMPLE 2:
local DATA = {
	Team1 = {{"NPC1"}, {"NPC2"}, {"NPC3"}, {"NPC4"}},
	Moves = {
		["NPC1"] = {"Bloxy Cola"},
		["NPC2"] = {Punch"},
		["NPC3"] = {"Bloxy Cola"},
		["NPC4"] = {"Punch"}
	}
}

--After i sorted THIS table it should be like this:

{NPC1, NPC3, NPC2, NPC4}

Actually, i don’t know any other good way to sort table, so i don’t think theres anything i can do, except fixing this one.