Please read the following article: https://www.lua.org/pil/19.3.html
The 2nd paragraph goes over that you can only use this on arrays, not dictionaries. What you are doing is using it on a dictionary.
They show an example of transfering a dictionary over to an array and then back. While using table.sort in between.
You need to realize that you’re using a dictionary, and this function works only on arrays.
Here’s an example on how to use it :
This will return you the numbers in order [larger to smaller]
local tab = {1,2,5,6,3}
table.sort(tab,function(a,b)
return a>b
end)
for a,b in pairs(tab) do
print(a,b)
end
Here is how you can use it in your case:
In this example, it’d return you the values in the table in order, from larger to smaller [it’ll detect the numbers in your values and work with that]
local mytab = {"Test1","Test2","Test3"}
table.sort(mytab,function(a,b)
return tonumber(string.match(a,"%d")) > tonumber(string.match(b,"%d"))
end)
for i,v in pairs(mytab) do
print(i,v)
end
In this example, it’d sort them accordingly from higher to lower.
local mytab = {
{"Test1",450};
{"Test2",56};
{"Test3",78};
}
table.sort(mytab, function(a,b)
return a[2] > b[2]
end)
for i,v in pairs(mytab) do
print(v[1], v[2])
end
Yes, wouldn`t even need to change anything.
Your table could have any number of things you would like, the Sorting is done by just comparing the Numbers and ordering the table, nothing else matters.