Module:table/numKeys: Difference between revisions

From Linguifex
Jump to navigation Jump to search
Created page with "local math_module = "Module:math" local pairs = pairs local sort = table.sort local function is_positive_integer(...) is_positive_integer = require(math_module).is_positive_integer return is_positive_integer(...) end --[==[ Given a table, return an array containing all positive integer keys, sorted either in numerical order, or using a custom `keySort` function.]==] return function(t, keySort) local nums, i = {}, 0 for k in pairs(t) do if is_positive_integer(k)..."
m 1 revision imported
 
(No difference)

Latest revision as of 11:22, 21 April 2026



local math_module = "Module:math"

local pairs = pairs
local sort = table.sort

local function is_positive_integer(...)
	is_positive_integer = require(math_module).is_positive_integer
	return is_positive_integer(...)
end

--[==[
Given a table, return an array containing all positive integer keys, sorted either in numerical order, or using a custom `keySort` function.]==]
return function(t, keySort)
	local nums, i = {}, 0
	for k in pairs(t) do
		if is_positive_integer(k) then
			i = i + 1
			nums[i] = k
		end
	end
	-- No need for [[Module:math/compare]], as the default is adequate for integers.
	sort(nums, keySort)
	return nums
end