Leaguepedia | League of Legends Esports Wiki
Advertisement

Edit the documentation or categories for this module.


local util_args = require('Module:ArgsUtil')
local util_table = require('Module:TableUtil')

local h = {}
function h.compareTwoArraysByKeys(a, b, keyTbl, i)
	if i > #keyTbl then return nil end
	local thisKey = keyTbl[i]
	if a[thisKey] == b[thisKey] then
		return h.compareTwoArraysByKeys(a, b, keyTbl, i+1)
	end
	h.compareNumberOrString(a[thisKey], b[thisKey])
end

function h.compareNumberOrString(a, b)
	if tonumber(x) and tonumber(y) then
		return tonumber(a) > tonumber(b)
	end
	return a > b
end

local p = {}

function p.sortListOfTablesByKeys(tbl, key, increasing)
	local keyTbl = util_table.guaranteeTable(key)
	table.sort(tbl,
		function(a,b)
			if not increasing then
				return h.compareTwoArraysByKeys(a, b, keyTbl, 1)
			else
				return h.compareTwoArraysByKeys(b, a, keyTbl, 1)
			end
		end)
end

function p.sortOrderedDictByKeys(tbl, key, increasing)
	local keyTbl = util_table.guaranteeTable(key)
	table.sort(tbl,
		function(a,b)
			if not increasing then
				local result = h.compareTwoArraysByKeys(tbl[a], tbl[b], keyTbl, 1)
				if result == nil then
					return a > b
				end
				return result
			else
				local result = h.compareTwoArraysByKeys(tbl[b], tbl[a], keyTbl, 1)
				if result == nil then
					return a < b
				end
				return result
			end
		end)
end

return p
Advertisement