From: Petr Špaček Date: Fri, 10 Jul 2020 20:09:46 +0000 (+0200) Subject: table_print: sort numbers by their value instead of lexicographic representation X-Git-Tag: v5.2.0~4^2~11 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bbc6aebcf96d789063a19f57c3da7fe1c5ebeaf9;p=thirdparty%2Fknot-resolver.git 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.. --- 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