Implemented ESX.Table.Sort() for pairs

This commit is contained in:
ElPumpo
2019-05-01 21:56:49 +02:00
parent d6323124c2
commit 47c81f36eb
+31
View File
@@ -125,4 +125,35 @@ function ESX.Table.Join(t, sep)
end
return str
end
-- Credit: https://stackoverflow.com/a/15706820
-- Description: sort function for pairs
function ESX.Table.Sort(t, order)
-- collect the keys
local keys = {}
for k,_ in pairs(t) do
keys[#keys + 1] = k
end
-- if order function given, sort by it by passing the table and keys a, b,
-- otherwise just sort the keys
if order then
table.sort(keys, function(a,b)
return order(t, a, b)
end)
else
table.sort(keys)
end
-- return the iterator function
local i = 0
return function()
i = i + 1
if keys[i] then
return keys[i], t[keys[i]]
end
end
end