Small cleanup, add reverse option

Properly add text at the end on non-reverse numeric
This commit is contained in:
DominikDoom
2023-09-13 19:39:48 +02:00
parent 783a847978
commit 2846d79b7d
2 changed files with 30 additions and 20 deletions

View File

@@ -214,26 +214,35 @@ function observeElement(element, property, callback, delay = 0) {
// Sort functions
function getSortFunction() {
let criterion = TAC_CFG.modelSortOrder || "Name";
const textSort = (a, b, reverse = false) => {
const textHolderA = a.type === ResultType.chant ? a.aliases : a.text;
const textHolderB = b.type === ResultType.chant ? b.aliases : b.text;
const aKey = a.sortKey || textHolderA;
const bKey = b.sortKey || textHolderB;
return reverse ? bKey.localeCompare(aKey) : aKey.localeCompare(bKey);
}
const numericSort = (a, b, reverse = false) => {
const noKey = reverse ? "-1" : Number.MAX_SAFE_INTEGER;
let aParsed = parseFloat(a.sortKey || noKey);
let bParsed = parseFloat(b.sortKey || noKey);
if (aParsed === bParsed) {
return textSort(a, b, false);
}
return reverse ? bParsed - aParsed : aParsed - bParsed;
}
return (a, b) => {
let textHolderA = a.type === ResultType.chant ? a.aliases : a.text;
let textHolderB = b.type === ResultType.chant ? b.aliases : b.text;
switch (criterion) {
case "Date Modified":
let aParsed = parseFloat(a.sortKey || "-1");
let bParsed = parseFloat(b.sortKey || "-1");
if (aParsed === bParsed) {
let aKey = a.sortKey || textHolderA;
let bKey = b.sortKey || textHolderB;
return aKey.localeCompare(bKey);
}
return bParsed - aParsed;
case "Date Modified (newest first)":
return numericSort(a, b, true);
case "Date Modified (oldest first)":
return numericSort(a, b, false);
default:
let aKey = a.sortKey || textHolderA;
let bKey = b.sortKey || textHolderB;
return aKey.localeCompare(bKey);
return textSort(a, b);
}
}
}