More sensible UI WI entries sorting - Use same secondary/tertiary sorting for all sortings - Refactor and streamline code

5e2e48c84d320ab91fdf544da51830d87d68e855

Wolfsblvt <wolfsblvt@gmail.com>

1 files changed, +24 -32Ignore whitespace
public/scripts/world-info.js+24 -32
@@ -1647,32 +1647,38 @@ function sortEntries(data) {
16471647
16481648 if (!data.length) return data;
16491649
1650+ /** @type {(a: any, b: any) => number} */
1651+ let primarySort;
1652+
1653+ // Secondary and tertiary it will always be sorted by Order descending, and last UID ascending
1654+ // This is the most sensible approach for sorts where the primary sort has a lot of equal values
1655+ const secondarySort = (a, b) => b.order - a.order;
1656+ const tertiarySort = (a, b) => a.uid - b.uid;
1657+
16501658 // If we have a search term for WI, we are sorting by weighting scores
16511659 if (sortRule === 'search') {
16521660 data.sort(primarySort = (a, b) => {
16531661 const aScore = worldInfoFilter.getScore(FILTER_TYPES.WORLD_INFO_SEARCH, a.uid);
16541662 const bScore = worldInfoFilter.getScore(FILTER_TYPES.WORLD_INFO_SEARCH, b.uid);
16551663 return (aScore - bScore);
16561664 });
16571665 }
16581666 else if (sortRule === 'custom') {
16591667 // First by display index, then by order, then by uid
16601668 data.sort(primarySort = (a, b) => {
16611669 const aValue = a.displayIndex;
16621670 const bValue = b.displayIndex;
1663-
1671+ return aValue - bValue;
1664- return (aValue - bValue || b.order - a.order || a.uid - b.uid);
1672+ };
1665- });
16661673 } else if (sortRule === 'priority') {
16671674 // First constant, then normal, then disabled. Then sort by order
16681675 data.sort(primarySort = (a, b) => {
16691676 const aValue = a.constant ? 0 : a.disable ? 2 : 1;
16701677 const bValue = b.constant ? 0 : b.disable ? 2 : 1;
1671-
1678+ return aValue - bValue;
1672- return (aValue - bValue || b.order - a.order);
1679+ };
1673- });
16741680 } else {
16751681 const primarySort = (a, b) => {
16761682 const aValue = a[sortField];
16771683 const bValue = b[sortField];
16781684
@@ -1690,26 +1696,12 @@ function sortEntries(data) {
16901696 // Sort numbers
16911697 return orderSign * (Number(aValue) - Number(bValue));
16921698 };
1693- const secondarySort = (a, b) => a.order - b.order;
1694- const tertiarySort = (a, b) => a.uid - b.uid;
1695-
1696- data.sort((a, b) => {
1697- const primary = primarySort(a, b);
1698-
1699- if (primary !== 0) {
1700- return primary;
1701- }
1702-
1703- const secondary = secondarySort(a, b);
1704-
1705- if (secondary !== 0) {
1706- return secondary;
1707- }
1708-
1709- return tertiarySort(a, b);
1710- });
17111699 }
17121700
1701+ data.sort((a, b) => {
1702+ return primarySort(a, b) || secondarySort(a, b) || tertiarySort(a, b);
1703+ });
1704+
17131705 return data;
17141706}
17151707