diff --git a/javascript/_utils.js b/javascript/_utils.js index 4050f29..de745f1 100644 --- a/javascript/_utils.js +++ b/javascript/_utils.js @@ -214,26 +214,35 @@ function observeElement(element, property, callback, delay = 0) { // Sort functions function getSortFunction() { let criterion = TAC_CFG.modelSortOrder || "Name"; + + const textSort = (a, b, reverse = false) => { + const textHolderA = a.type === ResultType.chant ? a.aliases : a.text; + const textHolderB = b.type === ResultType.chant ? b.aliases : b.text; + + const aKey = a.sortKey || textHolderA; + const bKey = b.sortKey || textHolderB; + return reverse ? bKey.localeCompare(aKey) : aKey.localeCompare(bKey); + } + const numericSort = (a, b, reverse = false) => { + const noKey = reverse ? "-1" : Number.MAX_SAFE_INTEGER; + let aParsed = parseFloat(a.sortKey || noKey); + let bParsed = parseFloat(b.sortKey || noKey); + + if (aParsed === bParsed) { + return textSort(a, b, false); + } + + return reverse ? bParsed - aParsed : aParsed - bParsed; + } + return (a, b) => { - let textHolderA = a.type === ResultType.chant ? a.aliases : a.text; - let textHolderB = b.type === ResultType.chant ? b.aliases : b.text; - switch (criterion) { - case "Date Modified": - let aParsed = parseFloat(a.sortKey || "-1"); - let bParsed = parseFloat(b.sortKey || "-1"); - - if (aParsed === bParsed) { - let aKey = a.sortKey || textHolderA; - let bKey = b.sortKey || textHolderB; - return aKey.localeCompare(bKey); - } - - return bParsed - aParsed; + case "Date Modified (newest first)": + return numericSort(a, b, true); + case "Date Modified (oldest first)": + return numericSort(a, b, false); default: - let aKey = a.sortKey || textHolderA; - let bKey = b.sortKey || textHolderB; - return aKey.localeCompare(bKey); + return textSort(a, b); } } } diff --git a/scripts/tag_autocomplete_helper.py b/scripts/tag_autocomplete_helper.py index 0af571a..2b6f69e 100644 --- a/scripts/tag_autocomplete_helper.py +++ b/scripts/tag_autocomplete_helper.py @@ -28,7 +28,8 @@ except Exception as e: # Not supported. # Sorting functions for extra networks / embeddings stuff sort_criteria = { "Name": lambda path, name, subpath: name.lower() if subpath else path.stem.lower(), - "Date Modified": lambda path, name, subpath: path.stat().st_mtime + "Date Modified (newest first)": lambda path, name, subpath: path.stat().st_mtime, + "Date Modified (oldest first)": lambda path, name, subpath: path.stat().st_mtime } def sort_models(model_list, sort_method = None, name_has_subpath = False): @@ -45,7 +46,7 @@ def sort_models(model_list, sort_method = None, name_has_subpath = False): sort_method = getattr(shared.opts, "tac_modelSortOrder", "Name") # Get sorting method from dictionary - sorter = sort_criteria[sort_method] if sort_criteria[sort_method] else sort_criteria['Name'] + sorter = sort_criteria.get(sort_method, sort_criteria["Name"]) # During merging on the JS side we need to re-sort anyway, so here only the sort criteria are calculated. # The list itself doesn't need to get sorted at this point. @@ -406,7 +407,7 @@ def on_ui_settings(): "tac_useLycos": shared.OptionInfo(True, "Search for LyCORIS/LoHa"), "tac_showWikiLinks": shared.OptionInfo(False, "Show '?' next to tags, linking to its Danbooru or e621 wiki page").info("Warning: This is an external site and very likely contains NSFW examples!"), "tac_showExtraNetworkPreviews": shared.OptionInfo(True, "Show preview thumbnails for extra networks if available"), - "tac_modelSortOrder": shared.OptionInfo("Name", "Model sort order", gr.Dropdown, lambda: {"choices": ["Name", "Date Modified"]}).info("Order for extra network models and wildcards in dropdown"), + "tac_modelSortOrder": shared.OptionInfo("Name", "Model sort order", gr.Dropdown, lambda: {"choices": list(sort_criteria.keys())}).info("Order for extra network models and wildcards in dropdown"), # Insertion related settings "tac_replaceUnderscores": shared.OptionInfo(True, "Replace underscores with spaces on insertion"), "tac_escapeParentheses": shared.OptionInfo(True, "Escape parentheses on insertion"),