This commit is contained in:
FlightControl
2017-06-07 12:56:43 +02:00
parent d120875fa9
commit ef95cfb1f5
8 changed files with 132 additions and 22 deletions

View File

@@ -303,3 +303,27 @@ function UTILS.DoString( s )
return false, err
end
end
-- Here is a customized version of pairs, which I called spairs because it iterates over the table in a sorted order.
function UTILS.spairs( 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