table.sort
requires you to give 2 things, a table to sort, and a function to used to determine the sorting used, where if it returns true
, it means the first element must come before the second by default, it uses <
. Which would look like this
table.sort(tbl,function(a,b)
return a < b
end)
Which does an ascending order sort. If it was >
, it would’ve done a descending order. My guess for how it works for strings is that it converts the string to ASCII decimals/numbers and then compares that number when sorting. This would also explain why in some cases capital letters are in places you wouldn’t expect them to be if you ever test that. Example
local tbl = {"B", "a", "g", "e"}
table.sort(tbl, function(a,b)
return a < b
end)
for i,v in pairs(tbl) do
print(v)
end
The result would give
B
a
e
g
Because in Ascii, the decimal of capital letters is smaller than lower case letters (B is 66 and b is 98).
But to answer your question, if you just do table.sort(Table)
, it should be enough for your usecase if you only use Capitals or lowercase. Or if you’re planning on using before, use a custom function that compares the lower case values via :lower()
as @Nodehayn stated, which sorry that I didn’t mention it at first, it’s late at nigh there
Please ask if you have more questions about table.sort
as I may’ve forgot to mention something