sort()
The table.sort()
function sorts a given table in place.
Syntax
table.sort(table, [comp])
The first argument, table
, is the table to be sorted. The second argument, comp
, is an optional comparison function that specifies the sorting order. If comp
is not provided, the default less-than operation is used.
Example
In this example, there is a table named numbers
. The table.sort()
function is used to sort the table in ascending order.
local numbers = {5, 3, 1, 4, 2}table.sort(numbers)for i, v in pairs(numbers) doprint(v)end
The code results in the following output:
12345
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.