Codectionary / Developer documentation / Luau

Luau ordered record lists

table.sort rearranges a sequence in place with a strict comparator. Secondary keys make tied results deterministic without relying on a stable sort.

Syntax

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

Examples

Luau ordered record lists

Run as a standalone current Luau script, or as a Script in Roblox Studio ServerScriptService. No Roblox engine APIs are required; type-annotated syntax is not standard Lua.

local rows = {{name = "Lin", score = 8}, {name = "Ada", score = 8}, {name = "Bo", score = 3}}
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
  1. Output: Ada, Lin, Bo. Scores descend, and equal scores use ascending names.

Best practices

  • Return false for equal values and keep comparators consistent. Sort a copy when another caller depends on the original order.

Runtime & versions

Luau host; Roblox engine examples require Roblox Studio. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

Luau documentation

Specifications & further reading