0Pricing
Lua Academy · Lesson

table.sort with Comparators

Sort tables with custom comparator functions and stable sort strategies.

table.sort with Comparators is a free Lua Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Lua Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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

Custom Comparator

Pass a function function(a, b) return a < b end as the second argument.

local words = {"banana", "apple", "cherry"}
table.sort(words, function(a, b) return a < b end)
print(table.concat(words, ", "))

Descending Sort

Reverse the comparison to sort in descending order.

local nums = {3, 1, 4, 1, 5, 9}
table.sort(nums, function(a, b) return a > b end)
print(table.concat(nums, ", "))

Sorting Tables by a Field

Sort an array of records by a specific field key.

local people = {
  {name="Charlie", age=30},
  {name="Alice", age=25},
  {name="Bob", age=28}
}
table.sort(people, function(a, b) return a.age < b.age end)
for _, p in ipairs(people) do print(p.name, p.age) end

Multi-Key Sort

Sort by a primary key, then by a secondary key for ties.

table.sort(people, function(a, b)
  if a.age ~= b.age then return a.age < b.age end
  return a.name < b.name
end)

Stability Note

Lua's table.sort is not guaranteed stable. Equal elements may appear in any order. Use an index tie-breaker for deterministic results.

Sorting Strings Case-Insensitively

Normalize with string.lower inside the comparator.

table.sort(words, function(a, b)
  return a:lower() < b:lower()
end)

Sorting with a Key Extractor

Cache extracted keys to avoid repeated computation during sort using a Schwartzian transform approach.

local function sortBy(t, keyFn)
  local keyed = {}
  for i, v in ipairs(t) do keyed[i] = {val=v, key=keyFn(v)} end
  table.sort(keyed, function(a, b) return a.key < b.key end)
  for i, item in ipairs(keyed) do t[i] = item.val end
end

Sorting Large Tables

For very large tables consider a Schwartzian transform: pair each element with its sort key, sort, then extract elements. This avoids recomputing the key on every comparison.

In-Place vs. Copy

table.sort mutates the original table. To sort a copy, first use table.move or iterate to clone, then sort the clone.

Comparator Question

Which comparator sorts numbers in ascending order?

Recap: table.sort

table.sort sorts in-place, accepts an optional comparator, is not stable, and is commonly used with field-based or multi-key strategies for complex ordering requirements.

Frequently asked questions

Is the “table.sort with Comparators” lesson free?

Yes — the full text of “table.sort with Comparators” is free to read here on the web, and the Lua Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Lua Academy course, upgrade to CoddyKit PRO.

What will I learn in “table.sort with Comparators”?

Sort tables with custom comparator functions and stable sort strategies. You practise Lua Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Lua Academy?

No prior experience is required. Lua Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “table.sort with Comparators” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Lua Academy lesson?

Yes. Every Lua Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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