Sorting Tables in Lua

Sometimes, you’ll need to sort a table in alphabetical or numerical order. Fortunately, Lua provides an easy function called table.sort(). This function will sort a table in alphabetical order.

Here’s how:

-- The original table
local cards = {"heart","spades","clubs","diamonds"}

-- Sort the table

-- Print the new contents of the table
for k,v in ipairs(cards) do

To sort a table, just pass in the table as a parameter to the function. And that’s it!

Daniel Williams

Leave a Reply

Your email address will not be published. Required fields are marked *