Hi. I’ve got a dictionary of key/value pairs with the values being tables, and before presenting these key/values to the user I want to sort them in the order level->rarity->quantity->alphabetical. You naturally cannot sort dictionaries, so I’m converting them into tables and then sorting them that way.
It all works pretty smooth, but for unknown reasons when the size of my data dictionary gets to around 4 my table.sort function starts erroring stating that one of the tables it’s comparing is nil. If I never insert a nil table, why is this happening? It’s totally unexpected behaviour to me, but I’m probably messing up somewhere. I’ve spent the best part of today tackling this problem but I think I need help, I’m new to table sorting!
My data dictionaries look something like so:
local data = {
["Black"] = {lev1 = 0, lev2 = 1};
["Gold"] = {lev1 = 1, lev2 = 0};
["White"] = {lev1 = 1, lev2 = 1};
["Yellow"] = {lev1 = 1, lev2 = 0};
};
I then pass all of the data into my sorting function, which returns a table of colours, their quantity, and their level in a sorted manner. Just so I don’t miss anything, I’m going to paste it in it’s entirety. Sorry if it’s a bit much!
local function returnOrderedItems(objects, cat) -- {["Black"],["White"]}, "Claws"
--[[data = {
["Black"] = {lev1 = 0, lev2 = 1};
["White"] = {lev1 = 1, lev2 = 0};
}]]
local toReturn = {}
local Lev1 = {}
local Lev2 = {}
for key, value in pairs (objects) do
if value.lev1 >= 1 then
print(key.." has a level 1.")
table.insert(Lev1,{colour = key, quantity = value.lev1, level = 1})
end
if value.lev2 >= 1 then
print(key.." is a level 2.")
table.insert(Lev2,{colour = key, quantity = value.lev2, level = 2})
end
end
local function sortPls(lev)
table.sort(lev, function(a, b)
if a == nil then
print("WHY IS TABLE SORTING AGAINST A NIL VALUE?")
end
if b == nil then
print("WHY IS TABLE SORTING AGAINST B NIL")
end
local clr1 = a.colour
local clr2 = b.colour
local ranks = definitions[cat] --[claws] for example.
local rank1 = 0
local rank2 = 0
for i, v in pairs (ranks) do
if v[clr1] ~= nil then
if i == "Common" then
rank1 = 1
elseif i == "Uncommon" then
rank1 = 2
elseif i == "Rare" then
rank1 = 3
elseif i == "Legendary" then
rank1 = 4
end
end
if v[clr2] ~= nil then
if i == "Common" then
rank2 = 1
elseif i == "Uncommon" then
rank2 = 2
elseif i == "Rare" then
rank2 = 3
elseif i == "Legendary" then
rank2 = 4
end
end
if rank1 ~= 0 and rank2 ~= 0 then break end
end
if rank1 > rank2 then print(clr1.." is a higher rank than "..clr2) return a elseif rank2 > rank1 then print(clr2.." is a higher rank than "..clr1) return b end
if a.quantity > b.quantity then print("Player owns more of "..clr1) return a elseif b.quantity > a.quantity then print("Player owns more of "..clr2) return b end
print("Both colours are at the same rank and same quantity!")
return a.colour<b.colour -- alphabetical
end)
end
sortPls(Lev1)
sortPls(Lev2)
for i, v in pairs (Lev2) do
table.insert(toReturn, v)
end
for i, v in pairs (Lev1) do
if i == 1 then
print("THE FIRST KEY OF lev1 SHOUDL BE THE HIGHEST :(")
print(v.key)
end
table.insert(toReturn, v)
end
return toReturn
end
The annoying thing is, I’m getting this output:
WHY IS TABLE SORTING AGAINST B NIL
This is then promptly followed by:
attempt to index nil with 'colour'
When setting the variable ‘clr2’ to ‘b.colour’. Because b is nil. But why is a nil value even there in the first place? Why has my table sort function just pulled it out of thin air?
Help!