local icu = require("justenoughicu")
local collatedSort = {
und = function (t, _, comparator)
if comparator then
table.sort(t, function (e1, e2)
return comparator(e1, e2, function (s1, s2)
return s1 < s2 and -1 or s1 > s2 and 1 or 0
end)
end)
else
table.sort(t)
end
end,
}
setmetatable(collatedSort, {
__call = function (self, t, options, comparator)
local lang = SILE.settings:get("document.language")
if self[lang] and type(self[lang]) == "function" then
return self[lang](t, options, comparator)
end
if self[lang] and type(self[lang]) == "table" then
options = options or self[lang]
end
local collator = icu.collation_create(lang, options or {})
local stringCompareClosure = function (s1, s2)
return icu.compare(collator, s1, s2)
end
table.sort(t, function (e1, e2)
if comparator then
return comparator(e1, e2, stringCompareClosure)
end
return stringCompareClosure(e1, e2) < 0
end)
icu.collation_destroy(collator)
end,
})
return collatedSort