This commit is contained in:
TheGiddyLimit
2024-01-01 19:34:49 +00:00
parent 332769043f
commit 8117ebddc5
1748 changed files with 2544409 additions and 1 deletions

View File

@@ -0,0 +1,61 @@
import {InitiativeTrackerConst} from "./dmscreen-initiativetracker-consts.js";
export class InitiativeTrackerSort {
static _getSortMultiplier ({sortDir}) {
return sortDir === InitiativeTrackerConst.SORT_DIR_DESC ? -1 : 1;
}
static _sortRowsNameBasic ({sortDir}, rowA, rowB) {
return this._getSortMultiplier({sortDir}) * SortUtil.ascSortLower(
rowA.entity.customName || rowA.entity.name || "",
rowB.entity.customName || rowB.entity.name || "",
);
}
static _sortRowsInitiativeBasic ({sortDir}, rowA, rowB) {
return this._getSortMultiplier({sortDir}) * SortUtil.ascSort(
rowA.entity.initiative || 0,
rowB.entity.initiative || 0,
);
}
static _sortRowsOrdinal (rowA, rowB) {
// (Ordinals are always sorted ascending)
return SortUtil.ascSort(rowA.entity.ordinal || 0, rowB.entity.ordinal || 0);
}
static _sortRowsId (rowA, rowB) {
// (IDs are always sorted ascending)
return SortUtil.ascSort(rowA.id, rowB.id);
}
static _sortRowsName ({sortDir}, rowA, rowB) {
return this._sortRowsNameBasic({sortDir}, rowA, rowB)
|| this._sortRowsOrdinal(rowA, rowB)
|| this._sortRowsInitiativeBasic({sortDir}, rowA, rowB)
|| this._sortRowsId(rowA, rowB); // Fallback on ID to guarantee stable sort
}
static _sortRowsInitiative ({sortDir}, rowA, rowB) {
return this._sortRowsInitiativeBasic({sortDir}, rowA, rowB)
|| this._sortRowsNameBasic({sortDir}, rowA, rowB)
|| this._sortRowsOrdinal(rowA, rowB)
|| this._sortRowsId(rowA, rowB); // Fallback on ID to guarantee stable sort
}
static getSortedRows ({rows, sortBy, sortDir}) {
const fnSort = sortBy === InitiativeTrackerConst.SORT_ORDER_ALPHA
? this._sortRowsName.bind(this, {sortDir})
: this._sortRowsInitiative.bind(this, {sortDir});
return [...rows].sort(fnSort);
}
/* -------------------------------------------- */
static sortInitiativeInfos (a, b) {
return SortUtil.ascSort(b.applicability, a.applicability)
|| SortUtil.ascSort(b.initiative, a.initiative)
|| SortUtil.ascSort(a.id, b.id); // Fallback on ID to guarantee stable sort
}
}