Difference between revisions of "Module:SortUtil"

From Leaguepedia | League of Legends Esports Wiki
Jump to: navigation, search
[checked revision][checked revision]
((via Mediawiker ST3))
((via Mediawiker ST3))
Line 50: Line 50:
 
return result
 
return result
 
end
 
end
  +
end)
  +
end
  +
  +
function p.sortConstantDictionary(tbl)
  +
table.sort(tbl,
  +
function(a,b)
  +
return h.compareNumberOrStrong(tbl[a], tbl[b])
 
end)
 
end)
 
end
 
end

Revision as of 14:40, 16 May 2019

To edit the documentation or categories for this module, click here.


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
	return h.compareNumberOrString(a[thisKey], b[thisKey])
end

function h.compareNumberOrString(a, b)
	if tonumber(a) and tonumber(b) 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

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

return p