Quantcast
Sort table with string keys - WoWInterface
Thread Tools Display Modes
07-07-22, 12:09 PM   #1
Kesselero
A Murloc Raider
Join Date: Jun 2022
Posts: 6
Question 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!

Last edited by Kesselero : 07-07-22 at 12:14 PM.
  Reply With Quote
07-07-22, 12:16 PM   #2
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,540
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
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.
  Reply With Quote
07-07-22, 12:20 PM   #3
Kesselero
A Murloc Raider
Join Date: Jun 2022
Posts: 6
That was quick! Thank you so much.

I really need to learn about pairs and ipairs
  Reply With Quote
07-07-22, 12:43 PM   #4
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,540
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.
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 07-07-22 at 08:49 PM.
  Reply With Quote
07-08-22, 11:46 AM   #5
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,861
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.
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

  Reply With Quote
07-08-22, 04:42 PM   #6
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,136
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.
__________________
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

WoWInterface » Developer Discussions » Lua/XML Help » Sort table with string keys

Thread Tools
Display Modes

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