| 1647 | | 1647 | |
| 1648 | if (!data.length) return data; | 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 | // If we have a search term for WI, we are sorting by weighting scores | 1658 | // 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 uid | 1667 | // 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; |
| 1663 | | 1671 | 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 order | 1674 | // 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; |
| 1671 | | 1678 | 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]; |
| 1678 | | 1684 | |