Skip to content

Commit

Permalink
[Minor] Add deepsort utility
Browse files Browse the repository at this point in the history
  • Loading branch information
vstakhov committed Sep 14, 2020
1 parent f2f389b commit c2a0484
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion lualib/lua_util.lua
Expand Up @@ -982,7 +982,9 @@ local function deepcopy(orig)
for orig_key, orig_value in next, orig, nil do
copy[deepcopy(orig_key)] = deepcopy(orig_value)
end
setmetatable(copy, deepcopy(getmetatable(orig)))
if getmetatable(orig) then
setmetatable(copy, deepcopy(getmetatable(orig)))
end
else -- number, string, boolean, etc
copy = orig
end
Expand All @@ -991,6 +993,25 @@ end
exports.deepcopy = deepcopy
--[[[
-- @function lua_util.deepsort(table)
-- params: {
- - table
-- }
-- Performs recursive in-place sort of a table
--]]
local function deepsort(tbl, sort_func)
local orig_type = type(tbl)
if orig_type == 'table' then
table.sort(tbl, sort_func)
for _, orig_value in next, tbl, nil do
deepsort(orig_value)
end
end
end
exports.deepsort = deepsort
--[[[
-- @function lua_util.shallowcopy(tbl)
-- Performs shallow (and fast) copy of a table or another Lua type
Expand Down

0 comments on commit c2a0484

Please sign in to comment.