Merge pull request #2569 from SillyTavern/more-sensible-wi-ui-sorting

More sensible UI WI entries sorting
This commit is contained in:
Cohee 2024-07-27 21:19:37 +03:00 committed by GitHub
commit 84c384f255
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 24 additions and 32 deletions

View File

@ -1647,32 +1647,38 @@ function sortEntries(data) {
if (!data.length) return data; if (!data.length) return data;
/** @type {(a: any, b: any) => number} */
let primarySort;
// Secondary and tertiary it will always be sorted by Order descending, and last UID ascending
// This is the most sensible approach for sorts where the primary sort has a lot of equal values
const secondarySort = (a, b) => b.order - a.order;
const tertiarySort = (a, b) => a.uid - b.uid;
// If we have a search term for WI, we are sorting by weighting scores // If we have a search term for WI, we are sorting by weighting scores
if (sortRule === 'search') { if (sortRule === 'search') {
data.sort((a, b) => { primarySort = (a, b) => {
const aScore = worldInfoFilter.getScore(FILTER_TYPES.WORLD_INFO_SEARCH, a.uid); const aScore = worldInfoFilter.getScore(FILTER_TYPES.WORLD_INFO_SEARCH, a.uid);
const bScore = worldInfoFilter.getScore(FILTER_TYPES.WORLD_INFO_SEARCH, b.uid); const bScore = worldInfoFilter.getScore(FILTER_TYPES.WORLD_INFO_SEARCH, b.uid);
return (aScore - bScore); return aScore - bScore;
}); };
} }
else if (sortRule === 'custom') { else if (sortRule === 'custom') {
// First by display index, then by order, then by uid // First by display index
data.sort((a, b) => { primarySort = (a, b) => {
const aValue = a.displayIndex; const aValue = a.displayIndex;
const bValue = b.displayIndex; const bValue = b.displayIndex;
return aValue - bValue;
return (aValue - bValue || b.order - a.order || a.uid - b.uid); };
});
} else if (sortRule === 'priority') { } else if (sortRule === 'priority') {
// First constant, then normal, then disabled. Then sort by order // First constant, then normal, then disabled.
data.sort((a, b) => { primarySort = (a, b) => {
const aValue = a.constant ? 0 : a.disable ? 2 : 1; const aValue = a.constant ? 0 : a.disable ? 2 : 1;
const bValue = b.constant ? 0 : b.disable ? 2 : 1; const bValue = b.constant ? 0 : b.disable ? 2 : 1;
return aValue - bValue;
return (aValue - bValue || b.order - a.order); };
});
} else { } else {
const primarySort = (a, b) => { primarySort = (a, b) => {
const aValue = a[sortField]; const aValue = a[sortField];
const bValue = b[sortField]; const bValue = b[sortField];
@ -1690,26 +1696,12 @@ function sortEntries(data) {
// Sort numbers // Sort numbers
return orderSign * (Number(aValue) - Number(bValue)); return orderSign * (Number(aValue) - Number(bValue));
}; };
const secondarySort = (a, b) => a.order - b.order;
const tertiarySort = (a, b) => a.uid - b.uid;
data.sort((a, b) => {
const primary = primarySort(a, b);
if (primary !== 0) {
return primary;
}
const secondary = secondarySort(a, b);
if (secondary !== 0) {
return secondary;
}
return tertiarySort(a, b);
});
} }
data.sort((a, b) => {
return primarySort(a, b) || secondarySort(a, b) || tertiarySort(a, b);
});
return data; return data;
} }