From bbc6aebcf96d789063a19f57c3da7fe1c5ebeaf9 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Petr=20=C5=A0pa=C4=8Dek?= Date: Fri, 10 Jul 2020 22:09:46 +0200 Subject: [PATCH] table_print: sort numbers by their value instead of lexicographic representation Side-effect is that keys are grouped by their type, so numbers come first and strings later.. --- daemon/lua/sandbox.lua.in | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/daemon/lua/sandbox.lua.in b/daemon/lua/sandbox.lua.in index ec33f2622..dfbf815be 100644 --- a/daemon/lua/sandbox.lua.in +++ b/daemon/lua/sandbox.lua.in @@ -511,7 +511,7 @@ local function funcsign(f) return "(" .. table.concat(func_args, ", ") .. ")" end -function table_print (tt, indent, done) +function table_print(tt, indent, done) done = done or {} indent = indent or 0 local result = "" @@ -521,7 +521,17 @@ function table_print (tt, indent, done) for k in pairs(unordered_tt) do table.insert(keys, k) end - table.sort(keys, function (a, b) return tostring(a) < tostring(b) end) + table.sort(keys, + function (a, b) + if type(a) ~= type(b) then + return type(a) < type(b) + end + if type(a) == 'number' then + return a < b + else + return tostring(a) < tostring(b) + end + end) local i = 0 return function() i = i + 1 -- 2.47.2