Sort them once per category to sort by. First the lowest priority, then next lowest, then highest priority. I.e. three table.sort calls with different comparison functions.
EDIT: Actually, table.sort might not be stable so multiple sorts might not work. Try it and see if it works tho.
EDIT: Actually this is not the best idea anyway. Just have your comparison function first check the high priority, then the next highest priority if they’re equal on the first one. E.g.
function sortByProperties(array, ...)
local properties = {...}
table.sort(array, function(a, b)
for _, prop in ipairs(properties) do
assert(a[prop.name] and b[prop.name])
--if a[prop] == b[prop] * prop.dir then continue end -- This is unncessary, but shows all three cases
if a[prop.name] > b[prop.name] * prop.dir then return false end
if a[prop.name] < b[prop.name] * prop.dir then return true end
end
end)
return true --They're equal on all properties
end
local itemSortProps = {
{name="Rarity", dir=-1},
{name="Price", dir=1},
}
local items = {
{Price=10, Rarity=1},
{Price=10, Rarity=2},
{Price=20, Rarity=1},
{Price=30, Rarity=-1},
}
sortByProperties(items, unpack(itemSortProps))
for _, item in ipairs(items) do
print(item.Price, item.Rarity)
end
Try changing the order of Rarity and Price in itemSortProps, you'll get different results.
Also I read somewhere that I cannot sort dictionaries, is this true?
I have a dictionary holding all the items and their values.
Could i get an example of how I would sort them by two categories?
I understand how I would sort it by one thing but I’m not sure how I would do it by two things.
or if you use Enum.SortOrder.Name then something like this maybe i’m not sure how large your prices get so i only padded the string with upto 6 leading zeros maybe you could use less or more depending on how large the prices get
Simple example of a neatly sorted array of items. To show it in a GUI, I suggest using a list/grid layout, then looping through the array and cloning a template with each item’s information. Or at least that’s how I’d do it.