Browse Source

[Minor] Improve the default sort function in deep_sort function

tags/2.7
Vsevolod Stakhov 3 years ago
parent
commit
df5db93673
1 changed files with 9 additions and 1 deletions
  1. 9
    1
      lualib/lua_util.lua

+ 9
- 1
lualib/lua_util.lua View File

@@ -1012,10 +1012,18 @@ exports.deepcopy = deepcopy
-- }
-- Performs recursive in-place sort of a table
--]]
local function default_sort_cmp(e1, e2)
if type(e1) == type(e2) then
return e1 < e2
else
return type(e1) < type(e2)
end
end

local function deepsort(tbl, sort_func)
local orig_type = type(tbl)
if orig_type == 'table' then
table.sort(tbl, sort_func)
table.sort(tbl, sort_func or default_sort_cmp)
for _, orig_value in next, tbl, nil do
deepsort(orig_value)
end

Loading…
Cancel
Save