Lua Academy · Lesson

table.sort with Comparators

Sort tables with custom comparator functions and stable sort strategies.

Lesson 1 of 413 steps

table.sort with Comparators is a free Lua Academy lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Lua Academy learning path, and your progress syncs across the web and the CoddyKit app. The Lua Academy course includes 4 lessons in total.

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.

Free to start

Learn Lua with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
40
Lessons
159

Frequently Asked Questions

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

Yes — any 3 lessons of the Lua Academy learning path, including “table.sort with Comparators”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Lua Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.

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