Thread Tools Display Modes
04-08-24, 09:54 AM   #1
Doctorbeefy
A Defias Bandit
Join Date: Apr 2024
Posts: 1
Sorting table help

I am trying to sort this table by ID but I can't seem to figure it out, is there anyone out there that can assist me? Thank you in advance.

Here is my code:
Lua Code:
  1. function ess(x)
  2.     if not ITEMSCRAPESTATS  then
  3.         ITEMSCRAPESTATS = {}
  4.     end
  5.    ES:SetHyperlink("item:"..x..":0:0:0:0:0:0:0")
  6.    for i=1, ES:NumLines() do
  7.        local text = _G["ESTooltipTextLeft"..i]:GetText()
  8.        if _G["ESTooltipTextLeft2"]:GetText() then
  9.        ITEMSCRAPESTATS[x] = ITEMSCRAPESTATS[x] or {}
  10.        table.insert(ITEMSCRAPESTATS[x], _G["ESTooltipTextLeft"..i]:GetText())
  11.  
  12.        else
  13.        ITEMSCRAPESTATS[x] = ITEMSCRAPESTATS[x] or {}
  14.        table.insert(ITEMSCRAPESTATS[x], "No Stats for Item Number")
  15.        end
  16.    end
  17.     if _G["ESTooltipTextLeft2"]:GetText() then
  18.       print("Scraping Stats for Item Number: "..x)
  19.       else
  20.       print("No Stats for Item Number: "..x)
  21.       end
  22. end

This is what ends up in the table:

Lua Code:
  1. ITEMSCRAPESTATS = {
  2.     [60002] = {
  3.         "Goldroar Signet", -- [1]
  4.         "Binds when picked up", -- [2]
  5.         "Unique-Equipped", -- [3]
  6.         "Finger", -- [4]
  7.         "+1 Strength", -- [5]
  8.         "Item Level 15", -- [6]
  9.         " ", -- [7]
  10.     },
  11.     [60004] = {
  12.         "Divining Rod", -- [1]
  13.         "Binds when picked up", -- [2]
  14.         "Ranged", -- [3]
  15.         "9 - 17 Fire Damage", -- [4]
  16.         "(8.7 damage per second)", -- [5]
  17.         "Requires Level 6", -- [6]
  18.         "Item Level 12", -- [7]
  19.         " ", -- [8]
  20.     },
  21.     [60001] = {
  22.         "Goldroar Band", -- [1]
  23.         "Binds when picked up", -- [2]
  24.         "Unique-Equipped", -- [3]
  25.         "Finger", -- [4]
  26.         "+1 Intellect", -- [5]
  27.         "Item Level 15", -- [6]
  28.         " ", -- [7]
  29.     },
  30.     [60003] = {
  31.         "Waterlogged Dirge", -- [1]
  32.         "Binds when picked up", -- [2]
  33.         "Two-Hand", -- [3]
  34.         "17 - 26 Damage", -- [4]
  35.         "(7.2 damage per second)", -- [5]
  36.         "+1 Strength", -- [6]
  37.         "+1 Stamina", -- [7]
  38.         "Requires Level 6", -- [8]
  39.         "Item Level 10", -- [9]
  40.         " ", -- [10]
  41.     },
  42.     [60000] = {
  43.         "Resilient Poncho", -- [1]
  44.         "Binds when picked up", -- [2]
  45.         "Back", -- [3]
  46.         "20 Armor", -- [4]
  47.         "+1 Spirit", -- [5]
  48.         "Requires Level 6", -- [6]
  49.         "Item Level 26", -- [7]
  50.         " ", -- [8]
  51.     },
  52. }

I would like it to sort like this:

Lua Code:
  1. ITEMSCRAPESTATS = {
  2.     [60000] = {
  3.         "Resilient Poncho", -- [1]
  4.         "Binds when picked up", -- [2]
  5.         "Back", -- [3]
  6.         "20 Armor", -- [4]
  7.         "+1 Spirit", -- [5]
  8.         "Requires Level 6", -- [6]
  9.         "Item Level 26", -- [7]
  10.         " ", -- [8]
  11.     },
  12.     [60001] = {
  13.         "Goldroar Band", -- [1]
  14.         "Binds when picked up", -- [2]
  15.         "Unique-Equipped", -- [3]
  16.         "Finger", -- [4]
  17.         "+1 Intellect", -- [5]
  18.         "Item Level 15", -- [6]
  19.         " ", -- [7]
  20.     }
  21.     [60002] = {
  22.         "Goldroar Signet", -- [1]
  23.         "Binds when picked up", -- [2]
  24.         "Unique-Equipped", -- [3]
  25.         "Finger", -- [4]
  26.         "+1 Strength", -- [5]
  27.         "Item Level 15", -- [6]
  28.         " ", -- [7]
  29.     },
  30.     [60003] = {
  31.         "Waterlogged Dirge", -- [1]
  32.         "Binds when picked up", -- [2]
  33.         "Two-Hand", -- [3]
  34.         "17 - 26 Damage", -- [4]
  35.         "(7.2 damage per second)", -- [5]
  36.         "+1 Strength", -- [6]
  37.         "+1 Stamina", -- [7]
  38.         "Requires Level 6", -- [8]
  39.         "Item Level 10", -- [9]
  40.         " ", -- [10]
  41.     }
  42.     [60004] = {
  43.         "Divining Rod", -- [1]
  44.         "Binds when picked up", -- [2]
  45.         "Ranged", -- [3]
  46.         "9 - 17 Fire Damage", -- [4]
  47.         "(8.7 damage per second)", -- [5]
  48.         "Requires Level 6", -- [6]
  49.         "Item Level 12", -- [7]
  50.         " ", -- [8]
  51.     },
  52. }
  Reply With Quote
04-08-24, 06:54 PM   #2
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,327
Tables can't be sorted by keys. This is a combination of how Lua tables are stored in memory and how WoW serializes SavedVars. Lua tables are stored in two components, an array list and a hashmap. Non-array keys are stored in memory by hash and are completely random.

You need to make ITEMSCRAPESTATS into an array and put the ItemID somewhere in each entry. For example, this keeps track of items already scanned using a lookup table and adds them to the array and sorts when each entry is created.
Lua Code:
  1. ITEMSCRAPESTATS = {};
  2. local ItemScrapeLookup = {};
  3.  
  4. local function ScrapeSort(a, b)
  5.     return a[1] < b[1];
  6. end
  7.  
  8. function ess(x)
  9.     ES:SetHyperlink("item:"..x..":0:0:0:0:0:0:0");
  10.     if _G["ESTooltipTextLeft2"]:GetText() then
  11.         if not ItemScrapeLookup[x] then
  12.             local dump = {x};
  13.             ItemScrapeLookup[x] = dump;
  14.             table.insert(ITEMSCRAPESTATS, dump);
  15.             table.sort(ITEMSCRAPESTATS, ScrapeSort);
  16.  
  17.             print("Scraping Stats for Item Number: " .. x);
  18.             for i=1, ES:NumLines() do
  19.                 table.insert(dump, _G["ESTooltipTextLeft" .. i]:GetText());
  20.             end
  21.         end
  22.     else print("No Stats for Item Number: " .. x); end
  23. end

Note: Ideally, you'll want to only call table.sort() once after you add all the entries. Calling it as each entry is added will eat up a lot of processing power.
__________________
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)

Last edited by SDPhantom : 04-08-24 at 07:15 PM.
  Reply With Quote

WoWInterface » AddOns, Compilations, Macros » AddOn Help/Support » Sorting table help


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off