| 1939 | 1939 | } |
| 1940 | 1940 | }); |
| 1941 | 1941 | |
| 1942 | 1942 | $('#world_apply_custom_sortingworld_apply_current_sorting').off('click').on('click', async () => { |
| 1943 | 1943 | const entryCount = Object.keys(data.entries).length; |
| 1944 | 1944 | const moreThan100 = entryCount > 100; |
| 1945 | 1945 | |
| 1946 | 1946 | let content = '<span>Apply your customcurrent sorting to the "Order" field. The Order values will go down from the chosen number.</span>'; |
| 1947 | 1947 | if (moreThan100) { |
| 1948 | 1948 | content += `<div class="m-t-1"><i class="fa-solid fa-triangle-exclamation" style="color: #FFD43B;"></i> More than 100 entries in this world. If you don't choose a number higher than that, the lower entries will default to 0.<br />(Usual default: 100)<br />Minimum: ${entryCount}</div>`; |
| 1949 | 1949 | } |
| 1950 | 1950 | |
| 1951 | 1951 | const result = await Popup.show.input('Apply CustomCurrent Sorting', content, '100', { okButton: 'Apply', cancelButton: 'Cancel' }); |
| 1952 | 1952 | if (!result) return; |
| 1953 | 1953 | |
| 1954 | 1954 | const start = Number(result); |
| 1955 | 1955 | if (isNaN(start) || start < 0) { |
| 1956 | 1956 | toastr.error('Invalid number: ' + result, 'Apply CustomCurrent Sorting'); |
| 1957 | 1957 | return; |
| 1958 | 1958 | } |
| 1959 | 1959 | if (start < entryCount) { |
| 1960 | 1960 | toastr.warning('A number lower than the entry count has been chosen. All entries below that will default to 0.', 'Apply CustomCurrent Sorting'); |
| 1961 | 1961 | } |
| 1962 | 1962 | |
| 1963 | | - let counter = 0; |
| 1963 | + // We need to sort the entries here, as the data source isn't sorted |
| 1964 | 1964 | for (const entryentries of= Object.values(data.entries)) {; |
| 1965 | | - const newOrder = Math.max(start - (entry.displayIndex ?? 0), 0); |
| 1965 | + sortEntries(entries); |
| 1966 | + |
| 1967 | + let updated = 0, current = start; |
| 1968 | + for (const entry of entries) { |
| 1969 | + const newOrder = Math.max(current--, 0); |
| 1966 | 1970 | if (entry.order === newOrder) continue; |
| 1967 | 1971 | |
| 1968 | 1972 | entry.order = newOrder; |
| 1969 | 1973 | setOriginalDataValue(data, entry.order, 'order', entry.order); |
| 1970 | 1974 | counterupdated++; |
| 1971 | 1975 | } |
| 1972 | 1976 | |
| 1973 | 1977 | if (counterupdated > 0) { |
| 1974 | 1978 | toastr.info(`Updated ${counterupdated} Order values`, 'Apply Custom Sorting'); |
| 1975 | 1979 | await saveWorldInfo(name, data, true); |
| 1976 | 1980 | updateEditor(navigation_option.previous); |
| 1977 | 1981 | } else { |