Syntax
table.sort(rows, function(a, b) return a.rank < b.rank end)Examples
Lua custom record sorting
Run with standalone Lua 5.4: lua example.lua. These scripts target Lua 5.4 rather than Roblox Luau.
local rows = {{name = "Lin", score = 9}, {name = "Ada", score = 9}, {name = "Bo", score = 4}}
table.sort(rows, function(a, b)
if a.score ~= b.score then return a.score > b.score end
return a.name < b.name
end)
for _, row in ipairs(rows) do print(row.name) end- Output: Ada, Lin, Bo. Higher scores come first and names order tied scores.
Best practices
- Use a strict comparator: compare(a, a) must be false. table.sort is not stable and expects a sequence without holes.
Runtime & versions
Lua interpreter or a host application. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
Lua reference manuals