| 1647 | 1647 | |
| 1648 | 1648 | if (!data.length) return data; |
| 1649 | 1649 | |
| 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 | 1658 | // If we have a search term for WI, we are sorting by weighting scores |
| 1651 | 1659 | if (sortRule === 'search') { |
| 1652 | 1660 | data.sort(primarySort = (a, b) => { |
| 1653 | 1661 | const aScore = worldInfoFilter.getScore(FILTER_TYPES.WORLD_INFO_SEARCH, a.uid); |
| 1654 | 1662 | const bScore = worldInfoFilter.getScore(FILTER_TYPES.WORLD_INFO_SEARCH, b.uid); |
| 1655 | 1663 | return (aScore - bScore); |
| 1656 | 1664 | }); |
| 1657 | 1665 | } |
| 1658 | 1666 | else if (sortRule === 'custom') { |
| 1659 | 1667 | // First by display index, then by order, then by uid |
| 1660 | 1668 | data.sort(primarySort = (a, b) => { |
| 1661 | 1669 | const aValue = a.displayIndex; |
| 1662 | 1670 | const bValue = b.displayIndex; |
| 1663 | | - |
| 1671 | + return aValue - bValue; |
| 1664 | | - return (aValue - bValue || b.order - a.order || a.uid - b.uid); |
| 1672 | + }; |
| 1665 | | - }); |
| 1666 | 1673 | } else if (sortRule === 'priority') { |
| 1667 | 1674 | // First constant, then normal, then disabled. Then sort by order |
| 1668 | 1675 | data.sort(primarySort = (a, b) => { |
| 1669 | 1676 | const aValue = a.constant ? 0 : a.disable ? 2 : 1; |
| 1670 | 1677 | const bValue = b.constant ? 0 : b.disable ? 2 : 1; |
| 1671 | | - |
| 1678 | + return aValue - bValue; |
| 1672 | | - return (aValue - bValue || b.order - a.order); |
| 1679 | + }; |
| 1673 | | - }); |
| 1674 | 1680 | } else { |
| 1675 | 1681 | const primarySort = (a, b) => { |
| 1676 | 1682 | const aValue = a[sortField]; |
| 1677 | 1683 | const bValue = b[sortField]; |
| 1678 | 1684 | |