Order table from largest to smallest?

Hello! I want to sort the following list into largest → smallest:

local doors = {
	["UserId"] = 50, --I want to sort by the value
}

table.sort(doors)
	
return doors

I cant seem to find any way of doing this, all others have seen to been sorting out this:

local doors = {5,1,5,6,7}

table.sort(doors)

return doors

When ever I do that, it sorts the table by UserId number, aka, the name for the value. Thanks to everyone that can help!

1 Like

This sorts the table as you want.

local t = {5,1,5,6,7}

table.sort(t, function(a, b)
	return a > b
end)

If you want to sort by a specific value

local t = {5,1,5,6,7}

table.sort(t, function(a, b)
	return a.value > b.value
end)
1 Like

Hello! sorry for late response is the a.value and b.value the exact thing I have to put in my code? Here is an example:

local doors = {
   ["548381833"] = 50 ,-- sort the 50
   ["3736935196"] = 250, -- sort the 250
}

table.sort(doors, function(a, b)
	return a.value > b.value -- this dont work
end)
	
print(doors)

Alright. I found a solution:

--sort all values in the doors list
local sorted = {}
for k, v in pairs(doors) do
	table.insert(sorted, {k, v})
end
	
table.sort(sorted, function(a,b)
	return a[2] > b[2]
end)
	
print("Sorted: ", sorted)
	
return sorted
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.