Leaguepedia | League of Legends Esports Wiki
[checked revision][checked revision]
((via Mediawiker ST3))
((via Mediawiker ST3))
 
(9 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
local util_args = require('Module:ArgsUtil')
 
local util_args = require('Module:ArgsUtil')
 
local util_table = require('Module:TableUtil')
 
local util_table = require('Module:TableUtil')
  +
  +
local lang = mw.getLanguage('en')
   
 
local h = {}
 
local h = {}
function h.compareTwoArraysByKeys(a, b, keyTbl, i)
+
function h.compareTwoArraysByKeys(a, b, keyTbl, incTbl, i)
 
if i > #keyTbl then return nil end
 
if i > #keyTbl then return nil end
 
local thisKey = keyTbl[i]
 
local thisKey = keyTbl[i]
if a[thisKey] == b[thisKey] then
+
if a[thisKey] == b[thisKey] or not a[thisKey] or not b[thisKey] then
return h.compareTwoArraysByKeys(a, b, keyTbl, i+1)
+
return h.compareTwoArraysByKeys(a, b, keyTbl, incTbl, i+1)
 
end
 
end
if tonumber(a[thisKey]) and tonumber(b[thisKey]) then
+
return h.compareNumberOrString(a[thisKey], b[thisKey], incTbl[i])
 
end
return tonumber(a[thisKey]) > tonumber(b[thisKey])
 
  +
else
 
  +
function h.compareNumberOrString(a, b, increasing)
return a[thisKey] > b[thisKey]
 
  +
if increasing then return h.compareNumberOrString(b, a, false) end
  +
if tonumber(a) and tonumber(b) then
 
return tonumber(a) > tonumber(b)
 
end
 
end
  +
return lang:caseFold(a) > lang:caseFold(b)
 
end
 
end
   
 
local p = {}
 
local p = {}
   
function p.sortListOfTablesByKeys(tbl, key, increasing)
+
function p.tablesByKeys(tbl, key, increasing)
 
local keyTbl = util_table.guaranteeTable(key)
 
local keyTbl = util_table.guaranteeTable(key)
  +
local incTbl = util_table.guaranteeTable(increasing) or {}
 
table.sort(tbl,
 
table.sort(tbl,
 
function(a,b)
 
function(a,b)
 
return h.compareTwoArraysByKeys(a, b, keyTbl, incTbl, 1)
if not increasing then
 
return h.compareTwoArraysByKeys(a, b, keyTbl, 1)
 
else
 
return h.compareTwoArraysByKeys(b, a, keyTbl, 1)
 
end
 
 
end)
 
end)
 
end
 
end
   
function p.sortOrderedDictByKeys(tbl, key, increasing)
+
function p.dictByKeys(tbl, key, increasing)
 
local keyTbl = util_table.guaranteeTable(key)
 
local keyTbl = util_table.guaranteeTable(key)
  +
local incTbl = util_table.guaranteeTable(increasing) or {}
 
table.sort(tbl,
 
table.sort(tbl,
 
function(a,b)
 
function(a,b)
 
local result = h.compareTwoArraysByKeys(tbl[a], tbl[b], keyTbl, incTbl, 1)
if not increasing then
 
 
if result == nil then
local result = h.compareTwoArraysByKeys(tbl[a], tbl[b], keyTbl, 1)
 
if result == nil then
+
return a < b
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
 
return result
 
end)
 
end
  +
  +
function p.sortConstantDictionary(tbl)
  +
table.sort(tbl,
  +
function(a,b)
  +
return h.compareNumberOrString(tbl[a], tbl[b])
 
end)
 
end)
 
end
 
end

Latest revision as of 18:37, 18 July 2019

Edit the documentation or categories for this module.


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

local lang = mw.getLanguage('en')

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

function h.compareNumberOrString(a, b, increasing)
	if increasing then return h.compareNumberOrString(b, a, false) end
	if tonumber(a) and tonumber(b) then
		return tonumber(a) > tonumber(b)
	end
	return lang:caseFold(a) > lang:caseFold(b)
end

local p = {}

function p.tablesByKeys(tbl, key, increasing)
	local keyTbl = util_table.guaranteeTable(key)
	local incTbl = util_table.guaranteeTable(increasing) or {}
	table.sort(tbl,
		function(a,b)
			return h.compareTwoArraysByKeys(a, b, keyTbl, incTbl, 1)
		end)
end

function p.dictByKeys(tbl, key, increasing)
	local keyTbl = util_table.guaranteeTable(key)
	local incTbl = util_table.guaranteeTable(increasing) or {}
	table.sort(tbl,
		function(a,b)
			local result = h.compareTwoArraysByKeys(tbl[a], tbl[b], keyTbl, incTbl, 1)
			if result == nil then
				return a < b
			end
			return result
		end)
end

function p.sortConstantDictionary(tbl)
	table.sort(tbl,
		function(a,b)
			return h.compareNumberOrString(tbl[a], tbl[b])
		end)
end

return p