Note: Lua will sort tables in place and will replace the original table.
The really cool thing about programming is that when it comes down to it, everything is a number. So, if we need to sort a (number-indexed) table, we can use the character number to do a nice alphabet sort of everything in it. We will not be talking about extended characters or Unicode today.
local cool_things = {eggbug, dog, Rain}
local function customsort_alphabetical(a, b)
return string.lower(a) < string.lower(b)
-- Capital letters have a lower character number,
-- so if we did not make everything check as lowercase,
-- Rain would sort to the top of the list.
end
table.sort(cool_things, customsort_alphabetical)
-- The table is now sorted as: cool_things = {dog, eggbug, Rain}
So, the table.sort function will work through our list. While working it picks up two things in our list and then makes them both lowercase. While holding eggbug and the now lowercase rain, it takes the first character of each, looks at the value of that character and then sorts it in the table. Since the value of e is less than r, eggbug sorts before rain.
You can read more here on the Programming in Lua website.
