How to sort a table of products based on price?

I know how to get the price of a product/pass, I just don’t know how to use table.sort properly.
I tried using a secondary value in the table (like a custom order argument), but still didn’t work.

-- List example:
--[[
		Donations = { 
			Robux5 = {
				Name = "5 Robux", 
				Description = "", 
				ID = 1573607207, 
				Color = ColorSequence.new({
					ColorSequenceKeypoint.new(0, Color3.new(0.223529, 0.764706, 0.717647)),
					ColorSequenceKeypoint.new(1, Color3.new(0.239216, 0.721569, 0.6))
				})
			},
			Robux10 = {
				Name = "10 Robux", 
				Description = "", 
				ID = 1573607253, 
				Color = ColorSequence.new({
					ColorSequenceKeypoint.new(0, Color3.new(0.223529, 0.764706, 0.717647)),
					ColorSequenceKeypoint.new(1, Color3.new(0.239216, 0.721569, 0.6))
				})
			},
--]]
	for ListName, List in pairs(AveloData.Store) do
		local NewList = Store.Lists.Template:Clone()
		NewList.Name = ListName
		NewList.Parent = Store.Lists
		NewList.Visible = true
			

-- Here is where I need to sort the list, in order of the price of the product or pass, using the same logic/code below with MarketplaceService.

table.sort(List,function()
-- no idea what to put here
end)


		for _, Item in pairs(List) do
			local ProductInfo = nil

			local IsPass, response = pcall(function()
				ProductInfo = MarketplaceService:GetProductInfo(tonumber(Item.ID), Enum.InfoType.GamePass)
			end)
			
-- If the response above was a failure, it was likely because it's a devproduct, now trying a devproduct check below
			if not IsPass then
				local succ, response = pcall(function()
					ProductInfo = MarketplaceService:GetProductInfo(Item.ID, Enum.InfoType.Product)
				end)		
			end
			
-- If it can't find the product, or something else, give up on this item
			if ProductInfo == nil then return end
			
-- Start Creation of the frame
			local NewItem = NewList.Template:Clone()

If anyone can help, it’d be really appreciated

please look at existing posts before making another one.

How to use table.sort? - Help and Feedback / Scripting Support - Developer Forum | Roblox

this uses the exact same kind of system using dictionarys so it should work.

Firstly you can’t sort dictionaries as they are indexed by strings and not numbers, so you need to convert your gamepass dictionary into an index based table like such below:

local ExampleTable = { -- fill this table with your gamepasses
	{
		"Gamepass1"; -- name of the gamepass
		250; -- price of the gamepass
	};
	{
		"Gamepass2";
		500;
	};
}

function Sorter(a, b) -- compares indexes to eachother
	return a[2] > b[2] -- i[2] refers to the 2nd index of the table content which in our case is the price
end

table.sort(ExampleTable, Sorter) -- "Gamepass2" will be the 1st index inside of ExampleTable after this is ran because it's price is the highest with 500
1 Like

Thank you, after a bit of tweaking to use the price after checking with marketplaceservice, got it working perfectly.

image

1 Like

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