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
1648 if (!data.length) return data;1648 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
1650 // If we have a search term for WI, we are sorting by weighting scores1658 // If we have a search term for WI, we are sorting by weighting scores
1651 if (sortRule === 'search') {1659 if (sortRule === 'search') {
1652 data.sort((a, b) => {1660 primarySort = (a, b) => {
1653 const aScore = worldInfoFilter.getScore(FILTER_TYPES.WORLD_INFO_SEARCH, a.uid);1661 const aScore = worldInfoFilter.getScore(FILTER_TYPES.WORLD_INFO_SEARCH, a.uid);
1654 const bScore = worldInfoFilter.getScore(FILTER_TYPES.WORLD_INFO_SEARCH, b.uid);1662 const bScore = worldInfoFilter.getScore(FILTER_TYPES.WORLD_INFO_SEARCH, b.uid);
1655 return (aScore - bScore);1663 return aScore - bScore;
1656 });1664 };
1657 }1665 }
1658 else if (sortRule === 'custom') {1666 else if (sortRule === 'custom') {
1659 // First by display index, then by order, then by uid1667 // First by display index
1660 data.sort((a, b) => {1668 primarySort = (a, b) => {
1661 const aValue = a.displayIndex;1669 const aValue = a.displayIndex;
1662 const bValue = b.displayIndex;1670 const bValue = b.displayIndex;
16631671 return aValue - bValue;
1664 return (aValue - bValue || b.order - a.order || a.uid - b.uid);1672 };
1665 });
1666 } else if (sortRule === 'priority') {1673 } else if (sortRule === 'priority') {
1667 // First constant, then normal, then disabled. Then sort by order1674 // First constant, then normal, then disabled.
1668 data.sort((a, b) => {1675 primarySort = (a, b) => {
1669 const aValue = a.constant ? 0 : a.disable ? 2 : 1;1676 const aValue = a.constant ? 0 : a.disable ? 2 : 1;
1670 const bValue = b.constant ? 0 : b.disable ? 2 : 1;1677 const bValue = b.constant ? 0 : b.disable ? 2 : 1;
16711678 return aValue - bValue;
1672 return (aValue - bValue || b.order - a.order);1679 };
1673 });
1674 } else {1680 } else {
1675 const primarySort = (a, b) => {1681 primarySort = (a, b) => {
1676 const aValue = a[sortField];1682 const aValue = a[sortField];
1677 const bValue = b[sortField];1683 const bValue = b[sortField];
16781684
@@ -1690,26 +1696,12 @@ function sortEntries(data) {
1690 // Sort numbers1696 // Sort numbers
1691 return orderSign * (Number(aValue) - Number(bValue));1697 return orderSign * (Number(aValue) - Number(bValue));
1692 };1698 };
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 });
1711 }1699 }
17121700
1701 data.sort((a, b) => {
1702 return primarySort(a, b) || secondarySort(a, b) || tertiarySort(a, b);
1703 });
1704
1713 return data;1705 return data;
1714}1706}
17151707