I need somewhat of an order for a dictionary

Hello, I am making a shop system that involves dictionaries. For the shop system I need the different items to have an order. I know dictionaries don’t have an order, so I don’t know how I should sort each item. If anyone has a better way that wont have too many compromises then that is greatly appreciated.
Thanks!

["Zone1"] = {
		["Swords"] = {
			["Dragon Sword"] = {
				["Price"]  = {
					["Currency"] = "Coins";
					["Amount"] = 500;
				};
				["Damage"] = 10;
				["Object"] = Swords["Dragon Sword"];
				["Index"] = 1;
			};
			["Mighty Sword"] = {
				["Price"]  = {
					["Currency"] = "Coins";
					["Amount"] = 3500;
				};
				["Damage"] = 10;
				["Object"] = Swords["Mighty Sword"];
				["Index"] = 2;
			};
			["Hypersteel"] = {
				["Price"]  = {
					["Currency"] = "Coins";
					["Amount"] = 25000;
				};
				["Damage"] = 10;
				["Object"] = Swords["Hypersteel"];
				["Index"] = 3;
			};
			["The Orcs Axeblade"] = {
				["Price"]  = {
					["Currency"] = "Coins";
					["Amount"] = 65000;
				};
				["Damage"] = 10;
				["Object"] = Swords["The Orcs Axeblade"];
				["Index"] = 4;
			}
		}
	};

Here is an example of the data I am working with.

2 Likes

Rather than having each sword be a dictionary entry, turn them into array entries, with Name as an additional entry to each sword.

If you need your dictionary structure to stay the same, then make a second array which dictates the order of the swords:

local swordOrder = {
    "Dragon Sword",
    "Mighty Sword",
    "Hypersteel",
    "The Orcs Axeblade"
}

This seems like a good Idea. Are there any other ways I could possibly use that would be less messy?
You need 2 tables for this method.

You can initialize a lookup table when the game starts and use table.sort to put swords that do the least damage at lower indexes and those that do more damage at higher indexes, then if you want to show the items you can just loop over them and create gui instances and the worst swords should be first. You could also just use table.sort and check the amount they cost if you wish to do that instead.

Here is some “example code” which may or may not work (I didn’t test it…)

local lookup = {}

local items = ... --// your list of items

for zoneName, zone in pairs(items) do
	--// initalize containers weapon types for this zone in their own table
	local zoneContainer = {}

	--// initialize the values for the containers for each weapon type for this zone.

	for weaponType, weapons in pairs(zone) do
		local weaponContainer = zoneContainer[weaponType]

		if not weaponContainer then
			--// create the container if it doesnt exist

			zoneContainer[weaponType] = {}
		end

		for weaponName, weapon in pairs(weapons) do
			--// new weapon object to hold the name aswell as its other properties so that it can be used in an array for sorting
			local weaponObject = {
				Name = weaponName
			}

			for statName, statValue in pairs(weapon) do
				weaponObject[statName] = statValue 
			end

			table.insert(zoneContainer[weaponType], weaponObject)
		end

		--// sort each weapon by its damage

		table.sort(zoneContainer[weaponType], function(weaponA, weaponB)
			return weaponA.Damage < weaponB.Damage
		end)
	end

	lookup[zoneName] = zoneContainer
end

and so the weakest sword in zone1 would be accessed by doing lookup["Zone1"]["Swords"][1]

2 Likes

If I were to use something like this would I keep it in a module and get this data whenever I need it?

If you wanted to. It’s up to you where you want to keep it.

Thanks! That is exactly what I needed :slight_smile: