0Pricing
Lua Academy · Lesson

Length and Sorting

Count and sort sequences.

Length and Sorting is a free Lua Academy lesson on CoddyKit — lesson 4 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.

The Length Operator

The # operator returns the number of items in a sequence.

For a clean 1-based table with three elements, #t is 3. The last item lives at index #t.

local list = {"a", "b", "c"}
print(#list)
print(list[#list])

Length in a Loop

Use #t as the upper bound of a numeric for loop to visit every index.

Since sequences are 1-based, the range is 1, #t.

local nums = {10, 20, 30, 40}
for i = 1, #nums do
  print(i, nums[i])
end

Empty Tables

An empty sequence has length 0.

That makes #t == 0 a quick way to check whether a list has any items.

local empty = {}
print(#empty)
if #empty == 0 then
  print("list is empty")
end

Length After Changes

When you add or remove items with the table functions, # updates accordingly.

Insert grows the count, remove shrinks it.

local list = {"a", "b"}
table.insert(list, "c")
print(#list)
table.remove(list)
print(#list)

Gaps Break Length

# is reliable only for sequences without holes. A gap can make the result undefined.

Stick to table.insert and table.remove so # stays trustworthy.

local good = {1, 2, 3}
print(#good)
table.insert(good, 4)
print(#good)

Sorting In Place

table.sort(t) reorders the sequence in place, smallest to largest by default.

It changes the original table rather than returning a new one.

local nums = {3, 1, 2}
table.sort(nums)
print(nums[1], nums[2], nums[3])

Sorting Strings

Strings sort alphabetically by default, comparing character by character.

After sorting, index 1 holds the first item in order.

local names = {"Cy", "Ann", "Bob"}
table.sort(names)
print(names[1], names[2], names[3])

Descending Order

Pass a comparison function to control order. Returning a > b sorts largest first.

The function decides whether a should come before b.

local nums = {3, 1, 4, 2}
table.sort(nums, function(a, b)
  return a > b
end)
print(nums[1], nums[2], nums[3], nums[4])

Custom Comparisons

Comparators let you sort by any rule, such as string length instead of alphabet.

Below, shorter words come first.

local words = {"hello", "hi", "howdy"}
table.sort(words, function(a, b)
  return #a < #b
end)
print(words[1], words[2], words[3])

Length Plus Sort

Combine the tools: sort a list, then read endpoints by position.

After ascending sort, the minimum is at index 1 and the maximum at index #t.

local data = {7, 2, 9, 4}
table.sort(data)
print("min:", data[1])
print("max:", data[#data])

Sort Then Iterate

A common pipeline: sort the sequence, then walk it with ipairs to print in order.

Sorting reorders the same 1-based table, so iteration follows the new order.

local scores = {50, 10, 30}
table.sort(scores)
for i, s in ipairs(scores) do
  print(i, s)
end

Quick Check

Recall what the length operator gives you.

Recap

You learned that #t counts items in a gap-free sequence, that the last item is at t[#t], and that table.sort reorders in place (ascending by default, or with a custom comparator).

Together with insert, remove, and ipairs, you can now manage Lua sequences confidently.

Frequently asked questions

Is the “Length and Sorting” lesson free?

Yes — the full text of “Length and Sorting” 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 “Length and Sorting”?

Count and sort sequences. 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 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Length and Sorting” 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. Arrays as Tables
  2. Adding and Removing Items
  3. Iterating with ipairs
  4. Length and Sorting
← Back to Lua Academy