Jump to content

Module:Table/keysToList

From The Mighty Kingdoms Wiki
Revision as of 08:44, 29 June 2025 by Cyrus (talk | contribs) (1 revision imported)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Documentation for this module may be created at Module:Table/keysToList/doc

local compare_module = "Module:compare"

local pairs = pairs
local sort = table.sort

local compare
local function get_compare()
	compare, get_compare = require(compare_module), nil
	return compare
end

--[==[
Returns a list of the keys in a table, sorted using either the default `table.sort` function or a custom `key_sort` function.

If there are only numerical keys, [[Module:table/numKeys]] is probably faster.]==]
return function(t, key_sort)
	local list, i = {}, 0
	for key in pairs(t) do
		i = i + 1
		list[i] = key
	end
	-- Use specified sort function, or otherwise `compare`.
	sort(list, key_sort == nil and (compare or get_compare()) or key_sort)
	return list
end