View Single Post
08-26-10, 05:23 AM   #25
Luzzifus
A Warpwood Thunder Caller
 
Luzzifus's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2007
Posts: 94
Originally Posted by SDPhantom View Post
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:
  1. local buttonIDs = {}
  2. for i, button in self:IterateButtons() do
  3.     local item = cargBags:GetItemInfo(button.bagID, button.slotID)
  4.     if item.link then
  5.         local _,_,tQ = GetItemInfo(item.link)
  6.         local _,cnt = GetContainerItemInfo(button.bagID, button.slotID)
  7.         buttonIDs[i] = { item.id, tQ, button, cnt }
  8.     else
  9.         buttonIDs[i] = { 0, -2, button, -1 }
  10.     end
  11. end
  12. QuickSort(buttonIDs) -- sorting call
And the corresponding sort function would be this:
lua Code:
  1. local QuickSort;
  2. do
  3.     local func = function(v1, v2)
  4.         if not v1 or not v2 then return (v1 and true or false) end
  5.         if v1[1] == 0 or v2[1] == 0 then
  6.             return v1[1] > v2[1] -- empty slots last
  7.         elseif v1[2] ~= v2[2] then
  8.             return v1[2] > v2[2] -- higher quality first
  9.         elseif v1[1] ~= v2[1] then
  10.             return v1[1] > v2[1] -- group identical item ids
  11.         else
  12.             return v1[4] > v2[4] -- full/larger stacks first
  13.         end
  14.     end;
  15.     QuickSort = function(tbl) table.sort(tbl, func) end
  16. end

Last edited by Luzzifus : 08-26-10 at 05:33 AM.
  Reply With Quote