How to order list items in numerical order

Hello. I currently am trying to pull passes along with all of their product information. I’m using this to put the product id along with the price of the product into a scrolling frame. I’m not sure how to list these in numerical order due to the way that list ordering works. If anyone could help that would be greatly appreciated.

Here is my code that create the frames and inputs the data.

		local passes = pullPasses()
		
		for _,v in allPasses:GetChildren() do
			if v:IsA("Frame") then
				v:Destroy()
			end
		end
		
		for i,v in pairs(passes) do
			local newFrame = passFrame:Clone()
			newFrame:SetAttribute("id", tonumber(i))
			newFrame.Amount.Text = "$" .. v.Product.PriceInRobux
			newFrame.IdText.Text = "id: " .. i
			newFrame.Name = v.Product.PriceInRobux
			newFrame.Parent = allPasses
			newFrame.Visible = true
		end

Thanks!

If the passes array is already in the order you want, just change the second pairs to ipairs.

If it’s not, you also want to sort it before iterating with whatever sorting function you want:

table.sort(passes, function(a, b)
  return a.Product.PriceInRobux < b.Product.PriceInRobux
end)

okay so this works to sort them, but it seems the for loops is not running. I added 2 prints and the table is printing in the correct order but the print inside of the for loop is not printing at all

		for _,v in allPasses:GetChildren() do
			if v:IsA("Frame") then
				v:Destroy()
			end
		end
		
		table.sort(passes, function(a, b)
			return a.Product.PriceInRobux < b.Product.PriceInRobux
		end)
		
		print(passes)
		
		for i,v in ipairs(passes) do
			
			print("test")
			
			local newFrame = passFrame:Clone()
			newFrame:SetAttribute("id", tonumber(i))
			newFrame.Amount.Text = "$" .. v.Product.PriceInRobux
			newFrame.IdText.Text = "id: " .. i
			newFrame.Name = v.Product.PriceInRobux
			newFrame.Parent = allPasses
			newFrame.Visible = true
		end

You could try putting a breakpoint in your code and stepping through it line by line to inspect the variables

i also just realized that it isnt sorting correctly either

You never actually said how you want to sort things, I just gave an example that would sort by price. Also, see what you can figure out with the debugger!

ohh sorry i forgot to mention that. but i did want to sort by price and when i looked in the table it showed that the first one was 30, the last one was 14, and one of the middle ones was 7500 which shouldn’t happen. also im ngl idk how to use debugger lol ima watch a tutorial or something.

Is this a dictionary you’re trying to sort? (are the indices non-numerical?)

In Lua dictionaries cannot be sorted due to how the data is stored internally. I don’t know the exact details as to why.

1 Like

yea it’s a dictionary. would there be any other way i could sort by the price of the item while also keeping track of id that goes along with the number?

the dictionary looks like this

                    ["48912959"] =  ▼  {
                       ["Creator"] =  ▶ {...},
                       ["Item"] =  ▶ {...},
                       ["Product"] =  ▼  {
                          ["BcRequirement"] = 0,
                          ["Id"] = 0,
                          ["IsForSale"] = true,
                          ["IsFree"] = false,
                          ["IsLimited"] = false,
                          ["IsLimitedUnique"] = false,
                          ["IsPublicDomain"] = false,
                          ["IsRental"] = false,
                          ["IsResellable"] = false,
                          ["PriceInRobux"] = 30,
                          ["RentalDurationInHours"] = 0,
                          ["SellerId"] = 0,
                          ["TotalPrivateSales"] = 0
                       },

Ahhh thank you @C_Sharper for asking that. I was assuming an array.

@MasterObstacles you’ll need to store the keys of your dictionary in an array, then sort it. Something like

local ids = {}
for k, v in passes do table.insert(ids, k) end

-- sort most to least expensive
local function comp(keyA, keyB)
  return passes[keyA].Product.PriceInRobux > passes[keyB].Product.PriceInRobux
end

table.sort(ids, comp)

Then you loop through your ids table instead of passes:

for _, id in ipairs(ids) do
  local pass = passes[id]
  -- add your frame stuff but use `id` instead of `i`
  -- and `pass` instead of `v`
end
3 Likes

Okay thank you this works. I apologize for not being clear enough