View Single Post
08-25-10, 12:56 PM   #24
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,327
Originally Posted by Luzzifus View Post
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 View Post
lua Code:
  1. local QuickSort;
  2. do
  3.     local func = function(v1, v2)
  4.         if v1[1] == 0 or v2[1] == 0 then -- Empty bag slot
  5.             return v1[1] > v2[1]; -- Put empty slots last
  6.         elseif v1[2] ~= v2[2] then
  7.             return v1[2] > v2[2]; -- Higher quality first
  8.         elseif v1[1] ~= v2[1] then
  9.             return v1[1] < v2[1];
  10.         else -- Compare stack counts
  11.             local _, c1 = GetContainerItemInfo(v1[3].bagID, v1[3].slotID);
  12.             local _, c2 = GetContainerItemInfo(v2[3].bagID, v2[3].slotID);
  13.             return c1 < c2;
  14.         end
  15.     end;
  16.     QuickSort = function(tbl) table.sort(tbl, func); end
  17. 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.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote