Sorting numbers 100 to 0?

How does one sort numbers from 100 to 0, including comma? I have no clue how to do this, but I got something set up I guess. I’m trying to sort LayoutOrder based on a “number” highest to lowest.

Module:

local RarityModule = {
	Percentage = {
		[100 > 90] = 1,
		[90 > 80] = 2,
		[80 > 70] = 3,
		[70 > 60] = 4,
		[60 > 50] = 5,
		[50 > 40] = 6,
		[40 > 30] = 7,
		[30 > 20] = 8,
		[20 > 10] = 9,
		[10 > 0] = 10, -- Layout order 10 if the number is under 10, unfortunate that it wouldn't see a difference between 0.37 and 4 :(
	}
}

return RarityModule

Function:

function SortingService:Order(loc, order, egg) -- loc = object location, order = which order type.
	if order == "Number" then
		for i,v in pairs(loc:GetChildren()) do
			if v.Name ~= "UIGridLayout" then
				local Precentage = EggModule[tostring(egg)].Rarities[v.Name] -- gets numbers from 0-100, including commas.
				-- how would I possibly sort the numbers from highest to lowest?

				v.LayoutOrder = --set looped item from highest to lowest using the module above.
			end
		end
	end
end
function SortingService:Order(loc, order, egg) -- loc = object location, order = which order type.
    if order == "Number" then
        local toSort = {}
		for i,v in pairs(loc:GetChildren()) do
			if v.Name ~= "UIGridLayout" then
                local Precentage = EggModule[tostring(egg)].Rarities[v.Name]
                table.insert(toSort, {data = v, p = Precentage})
			end
        end
        
        table.sort(toSort, function(a, b)
            return a.p > b.p
        end)

        for i,v in ipairs(toSort) do
            v.data.LayoutOrder = i
        end
	end
end

and you don’t need to use your module, it will sort that 3.7 will higher than 4

1 Like