This isn't a hard question, but a tough one because as there is no specific method for this, you'd have to take a huge way around to do this...
local alphabet = {"a", "b", "c", ... , "z"} --finish this off plz :3333
local strings = {"Cantelope", "Bear", "Apple"}
local orderedstrings = {}
function OrderStrings(t, n) --t is the table of strings, n is the position of the letter to be checked
local original = t
local ordered = {}
for i = 1, #alphabet do
local lettertable = {}
for i2 = 1, #t do
if t[i2]:sub(n, n) == alphabet[i] then
table.insert(lettertable, t[i2])
end
end
lettertable = OrderStrings(lettertable, n+1)
table.insert(ordered, lettertable)
end
return ordered
end
orderedstrings = OrderStrings(strings, 1) --this would return a lot of tables inside other tables, therefore, I'm going to unpack it into one solid table:
function UnpackTable(t, OrderedStrings)
local ta = OrderedStrings
for i = 1, #t do
table.insert(ta, UnpackTable(t[i], OrderedStrings)
end
return ta
end
local orderedstrings = UnpackTable(orderedstrings, {})
I wrote this real quick, so it is highly possible that there are a lot of mistakes in here, try to understand what I did and if there are questions, feel free to ask :) |