table Library

table.sort uses a quicksort variant: not stable, and the comparator must be a strict weak ordering (a < b, never <=, or you get "invalid order function for sorting" errors). For a stable sort, decorate with the original index.

local t = {5, 2, 8, 1}

table.insert(t, 9)                 -- append
table.insert(t, 1, 0)              -- insert at position
local last = table.remove(t)       -- remove the last element and return it
local first = table.remove(t, 1)   -- remove at position

table.sort(t)                                          -- ascending (in place, not stable!)
table.sort(t, function(a, b) return a > b end)         -- descending
print(table.concat(t, ", "))                           -- join (strings and numbers only)
print(table.concat({"a", "b", "c"}, "-", 2, 3))        --> b-c

local packed = table.pack(1, nil, 3)                   -- {1, nil, 3, n = 3}
print(table.unpack({1, 2, 3}))                         --> 1  2  3
print(table.unpack({1, 2, 3}, 2))                      --> 2  3

local moved = table.move({1, 2, 3}, 1, 3, 2, {})       -- copy elements 1..3 into a new table at index 2 (5.3+)
local copy = table.move(t, 1, #t, 1, {})               -- array copy