Skip to content

Commit

Permalink
fix(tablefunctions): sort non-table keys without converting to string (
Browse files Browse the repository at this point in the history
…#3029)

This prevents tables being sorted like {[1]=..., [10]=..., [100]=...}
  • Loading branch information
salinecitrine committed May 18, 2024
1 parent 7515e36 commit e1cbe10
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions common/tablefunctions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,21 @@ if not table.toString then
end

local function keyCmp(a, b)
a = tableToString(a)
b = tableToString(b)
return a < b
local ta = type(a)
local tb = type(b)

-- numbers always sort before other keys
-- compare pairs of numbers directly
-- for everything else, convert to string first
if ta == "number" and tb == "number" then
return a < b
elseif ta == "number" and tb ~= "number" then
return true
elseif tb == "number" and ta ~= "number" then
return false
else
return tableToString(a) < tableToString(b)
end
end

tableToString = function(tbl, options, _seen, _depth)
Expand Down

0 comments on commit e1cbe10

Please sign in to comment.