mirror of
https://github.com/PHIDIAS0303/ExpCluster.git
synced 2025-12-30 04:21:41 +09:00
Fix logic error on table sort function (#72)
* Fix logic error on table sort function * Fix injected logic error from previous change * Incorporate Cooldude2606's review comments Added ldoc Removed a couple of redundant boolean checks
This commit is contained in:
committed by
Cooldude2606
parent
f14dc833bb
commit
e482dbcd01
@@ -246,6 +246,28 @@ function table.deepcopy(object)
|
|||||||
return _copy(object)
|
return _copy(object)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--- Default table comparator sort function.
|
||||||
|
-- @local
|
||||||
|
-- @param x one comparator operand
|
||||||
|
-- @param y the other comparator operand
|
||||||
|
-- @return true if x logically comes before y in a list, false otherwise
|
||||||
|
local sortfunc =
|
||||||
|
function(x, y) --sorts tables with mixed index types.
|
||||||
|
local tx = type(x)
|
||||||
|
local ty = type(y)
|
||||||
|
if tx == ty then
|
||||||
|
if type(x) == 'string' then
|
||||||
|
return string.lower(x) < string.lower(y)
|
||||||
|
else
|
||||||
|
return x < y
|
||||||
|
end
|
||||||
|
elseif tx == 'number' then
|
||||||
|
return true --only x is a number and goes first
|
||||||
|
else
|
||||||
|
return false --only y is a number and goes first
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
--- Returns a copy of all of the values in the table.
|
--- Returns a copy of all of the values in the table.
|
||||||
-- @tparam table tbl the table to copy the keys from, or an empty table if tbl is nil
|
-- @tparam table tbl the table to copy the keys from, or an empty table if tbl is nil
|
||||||
-- @tparam[opt] boolean sorted whether to sort the keys (slower) or keep the random order from pairs()
|
-- @tparam[opt] boolean sorted whether to sort the keys (slower) or keep the random order from pairs()
|
||||||
@@ -267,19 +289,7 @@ function table.values(tbl, sorted, as_string)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
if sorted then
|
if sorted then
|
||||||
table.sort(valueset,
|
table.sort(valueset, sortfunc)
|
||||||
function(x, y) --sorts tables with mixed index types.
|
|
||||||
local tx = type(x) == 'number'
|
|
||||||
local ty = type(y) == 'number'
|
|
||||||
if tx == ty then
|
|
||||||
return x < y and true or false --similar type can be compared
|
|
||||||
elseif tx == true then
|
|
||||||
return true --only x is a number and goes first
|
|
||||||
else
|
|
||||||
return false --only y is a number and goes first
|
|
||||||
end
|
|
||||||
end
|
|
||||||
)
|
|
||||||
end
|
end
|
||||||
return valueset
|
return valueset
|
||||||
end
|
end
|
||||||
@@ -305,19 +315,7 @@ function table.keys(tbl, sorted, as_string)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
if sorted then
|
if sorted then
|
||||||
table.sort(keyset,
|
table.sort(keyset, sortfunc)
|
||||||
function(x, y) --sorts tables with mixed index types.
|
|
||||||
local tx = type(x) == 'number'
|
|
||||||
local ty = type(y) == 'number'
|
|
||||||
if tx == ty then
|
|
||||||
return x < y and true or false --similar type can be compared
|
|
||||||
elseif tx == true then
|
|
||||||
return true --only x is a number and goes first
|
|
||||||
else
|
|
||||||
return false --only y is a number and goes first
|
|
||||||
end
|
|
||||||
end
|
|
||||||
)
|
|
||||||
end
|
end
|
||||||
return keyset
|
return keyset
|
||||||
end
|
end
|
||||||
@@ -521,4 +519,4 @@ function table.keysort(tbl)
|
|||||||
local _tbl = {}
|
local _tbl = {}
|
||||||
for _,k in pairs(o) do _tbl[k] = tbl[k] end
|
for _,k in pairs(o) do _tbl[k] = tbl[k] end
|
||||||
return _tbl
|
return _tbl
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user