0Pricing
Lua Academy · Lesson

table.sort with Comparators

Sort tables with custom comparator functions and stable sort strategies.

What Is table.sort?

table.sort sorts a table in-place using a comparison function. Without a comparator it uses the < operator.

Default Ascending Sort

Call table.sort(t) on a table of numbers or strings to sort ascending.

local nums = {5, 2, 8, 1, 9}
table.sort(nums)
for _, v in ipairs(nums) do print(v) end

All lessons in this course

  1. table.sort with Comparators
  2. table.concat and table.move
  3. Deep Copy and Shallow Copy
  4. Transforming Tables: map, filter, reduce
← Back to Lua Academy