Hi, I have got a little for loop that goes through and gets the price of each item from a table. Any idea how I could sort what it returns from smallest price to biggest?
for i = num, 0, -1 do
local Asset = MarketPlaceService:GetProductInfo(userTShirts[i])
local assetPrice = Asset.PriceInRobux
end
local assets = {2, 1, 3, 5, 4, 6} -- anything in array
local rightArray = {}
local function sort(a)
for v, i in pairs(a) do
rightArray[v] = v
end
print(table.unpack(rightArray))
end
sort(assets)
Don’t think it’s right solution if you have items with same prices because if you print 11 for example, it will output 7 instead
Might not work cuz i am not in studio to check any synthax errors or anything
local table1 = {}
for i,v pairs(userTShirts) do
local Asset = MarketPlaceService:GetProductInfo(userTShirts[i])
local assetPrice = Asset.PriceInRobux
local assetName = Asset.Name
table1 = {assetName ,assetPrice}
end
--and sort
-- Your table of items with prices
local items = {
{name = "Item A", price = 10},
{name = "Item B", price = 5},
{name = "Item C", price = 15},
-- Add more items as needed
}
-- Function to compare items by price
local function compareByPrice(item1, item2)
return item1.price < item2.price
end
-- Sort the items by price
table.sort(items, compareByPrice)
-- Print the sorted items
for _, item in ipairs(items) do
print(item.name, item.price)
end