Originally Posted by Luzzifus
I wrote that recursive sorting function because I didn't find a way to use table.sort with my data structure. I was also aware of the stack overflow errors it produced but I didn't find a way around it. So I hope it's ok if I use your code as a replacement in cargbags_Nivaya too?
|
Go ahead, I had written my own sorting function before that worked perfectly for the only exception that it was a Lua function instead of native C code, so it had the constraints of running through the script engine instead of directly in code.
It took a comparison function like
table.sort() does without restriction, if a check returned
true, the values remained, otherwise, it swapped them.
The switch back to
table.sort() was ultimately to save CPU time. To handle the glitch with equal values, I would usually add a check against a unique number in each value. For items, this would be their location.
Originally Posted by Saiket
lua Code:
local QuickSort; do local func = function(v1, v2) if v1[1] == 0 or v2[1] == 0 then -- Empty bag slot return v1[1] > v2[1]; -- Put 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]; else -- Compare stack counts local _, c1 = GetContainerItemInfo(v1[3].bagID, v1[3].slotID); local _, c2 = GetContainerItemInfo(v2[3].bagID, v2[3].slotID); return c1 < c2; end end; QuickSort = function(tbl) table.sort(tbl, func); end end
|
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.