Jump to content

Module:Table/sortedPairs

From The Mighty Kingdoms Wiki
Revision as of 16:18, 24 April 2025 by Template>Theknightwho (Protected "Module:table/sortedPairs": Highly visible template/module ([Edit=Allow only template editors and administrators] (indefinite) [Move=Allow only template editors and administrators] (indefinite)))
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

local table_keys_to_list_module = "Module:table/keysToList"

local function keys_to_list(...)
	keys_to_list = require(table_keys_to_list_module)
	return keys_to_list(...)
end

--[==[
Iterates through a table, with the keys sorted using the keysToList function.

If there are only numerical keys, `export.sparseIpairs` is probably faster.]==]
return function(t, key_sort)
	local list, i = keys_to_list(t, key_sort), 0
	return function()
		i = i + 1
		local k = list[i]
		if k ~= nil then
			return k, t[k]
		end
	end
end