Help with Sorting in Global Leaderboard

Hello Everyone!

I am having trouble with something in my global leaderboard! As of now, I am sorting players based off of how much Cash they have. Considering that this is a Button Simulator game, I want the leaderboard to sort based off of their max stat.

As of now, here are the stats in the game (best to worst):

  • Super Rebirths
    -Rebirths
  • Cash

If a player has super rebirths, they should be at the top. If they have rebirths, they should be under, and cash should be at the bottom.

How can I do this?

Code so far:

local ContentProvider = game:GetService("ContentProvider")
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local dataStore = DataStoreService:GetOrderedDataStore("Production5")

local leaderboards = workspace:WaitForChild("Leaderboards")

local FormatNumber = require(ReplicatedStorage.FormatNumber.Simple)

local goldL = leaderboards:WaitForChild("1stPlaceStand").Dummy
local silverL = leaderboards:WaitForChild("2ndPlaceStand").Dummy
local bronzeL = leaderboards:WaitForChild("3rdPlaceStand").Dummy

local goldHum = goldL:WaitForChild("Humanoid")
local goldAnim = Instance.new("Animation")
goldAnim.AnimationId = "http://www.roblox.com/asset/?id=5937560570"
ContentProvider:PreloadAsync({goldAnim})
local goldAnimTrack = goldHum:LoadAnimation(goldAnim)
goldAnimTrack:Play()

local silverHum = goldL:WaitForChild("Humanoid")
local silverAnim = Instance.new("Animation")
silverAnim.AnimationId = "http://www.roblox.com/asset/?id=5918726674"
ContentProvider:PreloadAsync({silverAnim})
local silverAnimTrack = silverHum:LoadAnimation(silverAnim)
silverAnimTrack:Play()

local bronzeHum = goldL:WaitForChild("Humanoid")
local bronzeAnim = Instance.new("Animation")
bronzeAnim.AnimationId = "http://www.roblox.com/asset/?id=3333432454"
ContentProvider:PreloadAsync({bronzeAnim})
local bronzeAnimTrack = bronzeHum:LoadAnimation(bronzeAnim)
bronzeAnimTrack:Play()

local surfaceGUI = script.Parent
local sample = script:WaitForChild("Sample")
local sf = script.Parent.Frame:WaitForChild("List")
local ui = sf:WaitForChild("ListContent"):WaitForChild("Items")

task.wait(3)
while true do
	for i, plr in pairs(game.Players:GetPlayers()) do
		if plr.UserId > 0 then
			local w = plr.leaderstats.Cash.Value
			if w then
				pcall(function()
					dataStore:UpdateAsync(plr.UserId, function(oldVal)
						return tonumber(w)
					end)
				end)
			end
		end
	end
	local smallestFirst = false
	local numberToShow = 100
	local minValue = 50
	local maxValue = 10e30
	local pages = dataStore:GetSortedAsync(smallestFirst, numberToShow, minValue, maxValue)
	
	local top = pages:GetCurrentPage()
	local data = {}
	for a, b in ipairs(top) do
		local userId = b.key
		local points = b.value
		local username = "[Failed to load]"
		local s, e = pcall(function()
			username = game.Players:GetNameFromUserIdAsync(userId)
		end)
		if not s then
			warn("Error getting name for "..userId.." Error: "..e)
		end
		local image = game.Players:GetUserThumbnailAsync(userId, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size150x150)
		table.insert(data, {username, points, image})
	end
	
	for number, d in pairs(data) do
		local name = d[1]
		local val = d[2]
		local image = d[3]
		local color = Color3.new(1, 1, 1)
		id = Players:GetUserIdFromNameAsync(name)
		if number == 1 then
			color = Color3.new(0.866667, 0.866667, 0)
		elseif number == 2 then
			color = Color3.new(0.478431, 0.478431, 0.478431)
		elseif number == 3 then
			color = Color3.new(0.403922, 0.262745, 0.0196078)
		end
		local new = sample:Clone()
		
		new.Name = id  
		new.LayoutOrder = number
		new.Values.PlayerIMG.Image = image
		new.Values.Username.Text = name
		new.Values.Value.Text = FormatNumber.FormatCompact(val)
		new.ImageColor3 = color
		new.Parent = ui
		
		if number == 1 then
			local goldHumDesc = game.Players:GetHumanoidDescriptionFromUserId(id)
			goldL.Humanoid:ApplyDescription(goldHumDesc)
		elseif number == 2 then
			local silverHumDesc = game.Players:GetHumanoidDescriptionFromUserId(id)
			silverL.Humanoid:ApplyDescription(silverHumDesc)
		elseif number == 3 then
			local bronzeHumDesc = game.Players:GetHumanoidDescriptionFromUserId(id)
			bronzeL.Humanoid:ApplyDescription(bronzeHumDesc)
			end
	end
	task.wait()
	
	task.wait(100)
end

Thank you!

-GreeTree Gaming