Help with sorting dictionaries within an array

Hello. I wanted to know how to sort my table but I don’t know how to use table.sort() in this situation. I’ve looked through other posts but I couldn’t find anything similar to the structure of my table.

I was planning to sort these out by the price from the lowest value to the highest:

Table structure:

local gamepasses = {
	{
		["id"] = 000000000,
		["price"] = 10
	},
	
	{
		["id"] = 000000000,
		["price"] = 1
	},
	
	{
		["id"] = 000000000,
		["price"] = 5
	}
}

Desired Outcome

local gamepasses = {
	{
		["id"] = 000000000,
		["price"] = 1
	},

	{
		["id"] = 000000000,
		["price"] = 5
	},

	{
		["id"] = 000000000,
		["price"] = 10
	}
}

How would I go about doing this? Thanks in advance.

Use table.sort(), you can pass it a custom function:

table.sort(gamepasses, function(A,B) return A.price < B.price end)
2 Likes