Originally Posted by SDPhantom
It really makes me cringe seeing data function calls in a sort comparison function. I would rather have the info directly in the data structure. Then again, to have it make any sense would require an update in the whole codebase to accept the new structure. It barely makes sense now.
|
Adapting the data structure is pretty easy:
lua Code:
local buttonIDs = {}
for i, button in self:IterateButtons() do
local item = cargBags:GetItemInfo(button.bagID, button.slotID)
if item.link then
local _,_,tQ = GetItemInfo(item.link)
local _,cnt = GetContainerItemInfo(button.bagID, button.slotID)
buttonIDs[i] = { item.id, tQ, button, cnt }
else
buttonIDs[i] = { 0, -2, button, -1 }
end
end
QuickSort(buttonIDs) -- sorting call
And the corresponding sort function would be this:
lua Code:
local QuickSort;
do
local func = function(v1, v2)
if not v1 or not v2 then return (v1 and true or false) end
if v1[1] == 0 or v2[1] == 0 then
return v1[1] > v2[1] -- empty slots last
elseif v1[2] ~= v2[2] then
return v1[2] > v2[2] -- higher quality first
elseif v1[1] ~= v2[1] then
return v1[1] > v2[1] -- group identical item ids
else
return v1[4] > v2[4] -- full/larger stacks first
end
end;
QuickSort = function(tbl) table.sort(tbl, func) end
end