I have a table filled with random content that the user enters. I want my users to be able to quickly search this table, and one way to facilitate their search is to sort the table alphabetically. Initially, the table looked something like this:
myTable = {
Zebra = "black and white",
Apple = "I love them!",
Coin = "25cents"
}
I managed to implement the pairsByKeys () function, which allowed me to display the contents of tables in alphabetical order, but not to store them that way. Due to the way the search is configured, the table itself must be in alphabetical order.
function pairsByKeys (t, f)
local a = {}
for n in pairs(t) do
table.insert(a, n)
end
table.sort(a, f)
local i = 0 -- iterator variable
local iter = function () -- iterator function
i = i + 1
if a[i] == nil then
return nil
else
return a[i], t[a[i]]
end
end
return iter
end
- (, - ), , , . , : , , , , :
myTable = {
[1] = { Apple = "I love them!" },
[2] = { Coin = "25cents" },
[3] = { Zebra = "black and white" },
}
, , , - . , , . , :
SortFunc = function ()
local newtbl = {}
local t = {}
for title,value in pairsByKeys(myTable) do
newtbl[title] = value
tinsert(t,newtbl[title])
end
myTable = t
end
myTable - . ?