Sorts a table. Alias for the standard library function table.sort.
See also Lua library functions.
Signature:
sort(table [, comparator])
Arguments:
table- A table (number)comparator- A function to compare table elements during the sort; takes two arguments and returnstrueif the first argument should be ordered before the second in the sorted table; equivalent tofunction(a,b) return a < b endif omitted (function)
Examples:
aTable = {"a", "c", "g", "e", "b", "f", "d"}
sort(aTable)
print(aTable)
-- shows "a b c d e f g"
sort(aTable, function(a,b) return a>b end)
print(aTable)
-- shows "g f e d c b a"
complexTable = {
{
name = "Gorrok",
level = 80,
class = "WARRIOR",
}
{
name = "Spin",
level = 79,
class = "SHAMAN",
},
{
name = "Valiant",
level = 80,
class = "HUNTER",
}
}
sort(complexTable, function(a,b) return a.name < b.name end)
for _, info in ipairs(complexTable) do print(info.name) end
-- prints "Gorrok, Spin, Valiant"
sort(complexTable, function(a,b) return a.level < b.level end)
for _, info in ipairs(complexTable) do print(info.name) end
-- prints "Spin, Gorrok, Valiant" (or "Spin, Valiant, Gorrok"; order of equal elements is not defined)
sort(complexTable, function(a,b) return a.class < b.class end)
for _, info in ipairs(complexTable) do print(info.name) end
-- prints "Valiant, Spin, Gorrok"
This function is defined in the Lua standard libraries