[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 |
||
− | + | return h.compareNumberOrString(a[thisKey], b[thisKey], incTbl[i]) |
|
⚫ | |||
⚫ | |||
+ | |||
− | 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 |
||
⚫ | |||
end |
end |
||
+ | return lang:caseFold(a) > lang:caseFold(b) |
||
end |
end |
||
local p = {} |
local p = {} |
||
− | function p. |
+ | 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) |
||
⚫ | |||
⚫ | |||
⚫ | |||
− | else |
||
⚫ | |||
⚫ | |||
end) |
end) |
||
end |
end |
||
− | function p. |
+ | 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) |
||
⚫ | |||
− | if not increasing then |
||
⚫ | |||
− | local result = h.compareTwoArraysByKeys(tbl[a], tbl[b], keyTbl, 1) |
||
− | + | return a < b |
|
⚫ | |||
⚫ | |||
− | return result |
||
− | else |
||
− | local result = h.compareTwoArraysByKeys(tbl[b], tbl[a], keyTbl, 1) |
||
− | if result == nil then |
||
− | return a < b |
||
⚫ | |||
− | 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