View Single Post
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