so i made a gui thing that allows you to buy and sell your weapons, and i want the sort order to be based on the cost rather than the name. is that possible? am i stupid?
By default it is not possible, but what you could do is set each items name to the negative price, and set the grid layout to sort by name.
It should make the most expensive go to the top, if you want the least expensive to go to the top, make it the positive price.
thanks! i was originally thinking of just doing that but i figured roblox would have some kind of built-in thing that allowed you to use different stuff apart from layoutorder and name. apparently not
you don’t have to change the item name to sort by price. instead of sorting by name you can create a custom sorting function using a UIListLayout or UIGridLayout. you can use a script to sort the items based on their price values directly by modifying the parents children. here’s a basic way to do it:
- store each item’s price in a Value object like a NumberValue.
- use a script to sort the items based on the Value objec, comparing the price.
this way, you can control the sort order based on price while keeping the item names intact. hope that helps!if you need more info tell me
Use the LayoutOrder property of each frame/whatever is a sibling of the UIGridLayout, that way you don’t have to modify the naming or anything, eg:
local frames = {}
for _, frame in parent:GetChildren() do
if frame:IsA('Frame') then -- or whatever criteria
table.insert(frames, frame)
end
end
-- assuming frames' names correspond to what they are containing, sort them
table.sort(frames, function(a, b)
return prices[a.Name] < prices[b.Name]
end)
for index, frame in frames do
frame.LayoutOrder = index
end
exactly like this and you dont have to alter the name.