WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Sort table with string keys (https://www.wowinterface.com/forums/showthread.php?t=59166)

Kesselero 07-07-22 12:09 PM

Sort table with string keys
 
Hey, so I've been trying to sort a table. The table contains various item information and looks like this.

Code:

ItemTable = {
        {
                ["ItemName"] = "Edgewalker Longboots",
                ["ItemID"] = 28545,
                ["ItemLink"] = "|cffa335ee|Hitem:28545:2657:24028:24061:::::70:::::::::|h[Edgewalker Longboots]|h|r",
        }, -- [1]
        {
                ["ItemName"] = "Wool Cloth",
                ["ItemID"] = 2911,
                ["ItemLink"] = "|cffffffff|Hitem:2592::::::::70:::::::::|h[Wool Cloth]|h|r",
        }, -- [2]
        {
                ["ItemName"] = "Adamantite Powder",
                ["ItemID"] = 2592,
                ["ItemLink"] = "|cffffffff|Hitem:24243::::::::70:::::::::|h[Adamantite Powder]|h|r",
        }, -- [3]
...
}

I've been trying to sort the table alphabetically after the ItemName.

Code:

local sorted_table = {}
    for i=1, #ItemTable do
    sorted_table[i] = ItemTable[i]["ItemName"]
    end
table.sort(sorted_table)

sorted_table now contains the item names in the right order but my problem has been to insert those values back into ItemTable at the right place, while still matching the other keys.

I spent some time googling and I'm still not sure if its even possible when using strings for an index but I hope it is, because I would like the table to look that way.

Thanks!

Fizzlemizz 07-07-22 12:16 PM

Lua Code:
  1. ItemTable = {
  2.     {
  3.         ["ItemName"] = "Edgewalker Longboots",
  4.         ["ItemID"] = 28545,
  5.         ["ItemLink"] = "|cffa335ee|Hitem:28545:2657:24028:24061:::::70:::::::::|h[Edgewalker Longboots]|h|r",
  6.     }, -- [1]
  7.     {
  8.         ["ItemName"] = "Wool Cloth",
  9.         ["ItemID"] = 2911,
  10.         ["ItemLink"] = "|cffffffff|Hitem:2592::::::::70:::::::::|h[Wool Cloth]|h|r",
  11.     }, -- [2]
  12.     {
  13.         ["ItemName"] = "Adamantite Powder",
  14.         ["ItemID"] = 2592,
  15.         ["ItemLink"] = "|cffffffff|Hitem:24243::::::::70:::::::::|h[Adamantite Powder]|h|r",
  16.     }, -- [3]
  17. }
  18.  
  19. sort(ItemTable, function(a, b)
  20.     return a.ItemName < b.ItemName -- or > depending on desired sort order
  21. end)
  22.  
  23. for k, v in ipairs(ItemTable) do
  24.     print(k, v.ItemName)
  25. end

Kesselero 07-07-22 12:20 PM

That was quick! Thank you so much.

I really need to learn about pairs and ipairs :D

Fizzlemizz 07-07-22 12:43 PM

Tables with string keys are unordered but you can use pairs to return the key/values in whatever order they're found in.

ipairs is for a numerically orderd tables, the same as
Lua Code:
  1. for i=1, #table do
  2.    -- return i, table[i] -- figurative return
  3. end

Lua Code:
  1. for key, value in pairs(stringKeyedTable) do
  2.     print(key, value)
  3. end
  4.  
  5. for key, value in ipairs(numericKeyedTable) do
  6.     print(key, value)
  7. end
In your table, ItemTable, contains a list of sub-tables in numeric order. The sort, resorted them based on the ItemName key each sub-table has.

Seerah 07-08-22 11:46 AM

To reiterate and clarify on what Fizzlemizz said, tables with strings as keys are stored in a random order. When you use pairs() to print the key,value pairs in a certain order, you're not actually sorting the table, it's just scanning through and reading/printing them in whatever order you define.

SDPhantom 07-08-22 04:42 PM

It was an answer to a second question, which was to explain what ipairs() and pairs() did. The original question was different than what the title suggested, which was actually how to sort a table of tables.


All times are GMT -6. The time now is 02:32 AM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI