wowpedia
Main Menu


Lua functions

Sort the array portion of a table in-place (i.e. alter the table).

table.sort(table [, compFunc])
sort(table[, compFunc])

Arguments

table
Table - Table the array portion of which you wish to sort.
compFunc
Optional Function - Comparison operator function; the function is passed two arguments (a, b) from the table, and should return a boolean value indicating whether a should appear before b in the sorted array. If omitted, the following comparison function is used:
function (a, b)
 return a < b
end

Example

> t = { 3,2,5,1,4 }
> table.sort(t)
> = table.concat(t, ", ")  -- display sorted values
1, 2, 3, 4, 5

A comparison function can be provided to customise the element sorting. For instance, to sort the elements in descending order:

> function isGreaterThan(a, b) return a > b end
> table.sort(t, isGreaterThan)
> = table.concat(t, ", ")
5, 4, 3, 2, 1

Details

See also