local math_compare_module = "Module:math/compare"
local string_compare_module = "Module:string/compare"
local table_compare_module = "Module:table/compare"
local require = require
local type = type
local types
local function get_types()
types, get_types = {
= "\1",
= "\2",
= "\3",
= "\4",
}, nil
return types
end
local compare_funcs
local function get_compare_funcs()
compare_funcs = {}
function compare_funcs.boolean(a, b)
return a == true and b == false
end
function compare_funcs.number(...)
local math_compare = require(math_compare_module)
compare_funcs.number = math_compare
return math_compare(...)
end
function compare_funcs.string(...)
local string_compare = require(string_compare_module)
compare_funcs.string = string_compare
return string_compare(...)
end
function compare_funcs.table(...)
local table_compare = require(table_compare_module)
compare_funcs.table = table_compare
return table_compare(...)
end
get_compare_funcs = nil
return compare_funcs
end
--[==[
A general comparison function, which returns {true} if {a} sorts before {b}, or otherwise {false}; it can be used as the sort function with {table.sort}.
This function is roughly equivalent to the {<} operator, but is capable of comparing a mix of types, using the following rules:
* When items of different types are compared, the type-order is {"nil"}, {"boolean"}, {"number"}, {"string"}, then alphabetical order by type name for all other types.
* Numbers are compared with ].
* Strings are compared with ].
* Tables are compared with ].
* Boolean {true} sorts before {false}.
* All other types always return {false}.]==]
return function(a, b)
local type_a, type_b = type(a), type(b)
if type_a == type_b then
local func = (compare_funcs or get_compare_funcs())
return func and func(a, b) or false
end
return ((types or get_types()) or type_a) < (types or type_b)
end