local Painters = {
["Philipceo90"] = 5;
["Test2"] = 10;
}
This is just a test table but i want the player with the highest value to be higher on the list
local Painters = {
["Philipceo90"] = 5;
["Test2"] = 10;
}
This is just a test table but i want the player with the highest value to be higher on the list
Could you do something like this ?
local Painters = {
[1] = {"Player1", 5}; --[TableOrder] = {"PlayerName", Value}
[2] = {"Player2", 10};
[3] = {"Player3", 7};
[4] = {"Player4", 16};
}
table.sort(Painters, function(a, b) --Sort the table in good order depending of values
return a[2] > b[2]
end)
for Index, Value in pairs(Painters)do --Print to check if it work
print(Index.. " = ".. Value[1].. "(".. Value[2].. ")")
end
here’s a way that works but I don’t know if there’s better
function partition(array, left, right, pivotIndex)
local pivotValue = array[pivotIndex]
array[pivotIndex], array[right] = array[right], array[pivotIndex]
local storeIndex = left
for i = left, right-1 do
if array[i] <= pivotValue then
array[i], array[storeIndex] = array[storeIndex], array[i]
storeIndex = storeIndex + 1
end
array[storeIndex], array[right] = array[right], array[storeIndex]
end
return storeIndex
end
function quicksort(array, left, right)
if right > left then
local pivotNewIndex = partition(array, left, right, left)
quicksort(array, left, pivotNewIndex - 1)
quicksort(array, pivotNewIndex + 1, right)
end
end
array = { 1, 5, 2, 17, 11, 3, 1, 22, 2, 37 }
quicksort(array, 1, #array)
for _, v in pairs(array) do
print(v)
end
It is a sorting algorithm called quicksort
well the painters wont be there by default instead the script will add them
Well, you still can update the table at any time.
local PlayerService = game:GetService("Players")
local CurrentPainters = 0
local Painters = {}
------------------------------------------------
PlayerService.PlayerAdded:Connect(function(Player)
CurrentPainters += 1
Painters[CurrentPainters] = {Player.Name, Value?}
end)
PlayerService.PlayerRemoving:Connect(function(Player)
CurrentPainters -= 1
for Index, Value in pairs(Painters)do
if Value[1] == Player.Name then
Index = nil
end
end
end)
------------------------------------------------
task.wait(5)
table.sort(Painters, function(a, b)
return a[2] > b[2]
end)
for Index, Value in pairs(Painters)do
print(Index.. " = ".. Value[1].. "(".. Value[2].. ")")
end
-- Define a function to sort a table in descending order based on values
local function sortTableDescending(inputTable)
local sortedTable = {}
-- Convert the table to a list of {user, value} pairs
for user, value in pairs(inputTable) do
if type(value) == "number" and value == value then -- Check for NaN
table.insert(sortedTable, {user = user, value = value})
end
end
-- Sort the list based on values in descending order
table.sort(sortedTable, function(a, b)
return a.value > b.value
end)
return sortedTable
end
-- Original table with user values
local robloxTable = {
User1 = 5,
User2 = 1,
User3 = 10,
Philipceo90 = 100,
User4 = "Invalid", -- Non-numeric value example
JamesMadison4 = nil, -- Nil value example
BenjaminFranklin1776 = 5, -- Duplicate value example
ThomasJefferson1801 = 0 / 0 -- NaN value example
}
-- Sort the table in descending order and print the results
local sortedUserList = sortTableDescending(robloxTable)
-- Check if the sorted list is not empty
if #sortedUserList > 0 then
for _, entry in ipairs(sortedUserList) do
print(entry.user .. " has a value of " .. entry.value)
end
else
print("No valid entries to sort.")
end
Can you simplify this
Extra text…
just use this
I don’t recommend it. The module is old and is not useful for this OP’s use case. Depending on the specific scenario, it may be beneficial to simply iterate over the dictionary in question and find the highest value and corresponding key.
Lua’s built-in table.sort
uses quicksort already, and it will be considerably faster than using a quicksort implementation written in Lua.
This is a dictionary not a table so table.sort won’t run on this
you can setup something like this and it will work using a table to hold each players data then accessing the location of the values to compare
local Painters = {
{'BOB', 10},
{'Jane', 5},
{'Test3', 70},
}
print(Painters)
table.sort(Painters, function(a,b)
return a[2] > b[2]
end)
print(Painters)