How to sort a dictionary from latest addition from newly added?

Heres an example:

local inventory = {
     ["M16"] = 2 --amount
}

inventory["Iron Ore"] = 1 --add iron ore

It will put Iron Ore above M16, which I don’t want, I want it to be below.
How would I begin doing this?

Try using table.insert to add to your table instead. For the first parameter, put your table, and for the second parameter, put the position where you want the value to be in.

It’s a dictionary, table.insert does not work on dictionaries.

Sorry, my bad. That was a stupid mistake. Anyways, here’s some code that should, in theory, work for dictionary sorting.

local Dictionary = {
	["AK47"] = 1,
}

local Order = {}

function addToDictionary(Key, Value)
	Dictionary[Key] = Value
	table.insert(Order, Key)
end

function getOrderedKeys()
 	return Order
end

addToDictionary("a", 100)
addToDictionary("b", 200)
addToDictionary("c", 33003)

local OrderedKeys = getOrderedKeys()
2 Likes

Thanks, it worked! Appreciate it

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.