From 5cea69472f45decbbc96ad9d37b882cdff4357a3 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Fri, 2 Jun 2023 17:43:56 +0800 Subject: [PATCH] Use natural sort --- src/webui/www/private/scripts/client.js | 12 ++++-------- src/webui/www/private/scripts/dynamicTable.js | 14 ++++++-------- src/webui/www/private/scripts/misc.js | 3 +++ 3 files changed, 13 insertions(+), 16 deletions(-) diff --git a/src/webui/www/private/scripts/client.js b/src/webui/www/private/scripts/client.js index e9840e66b..d667b6329 100644 --- a/src/webui/www/private/scripts/client.js +++ b/src/webui/www/private/scripts/client.js @@ -623,16 +623,12 @@ window.addEvent('load', function() { // Sort trackers by hostname const sortedList = [...trackerList.entries()].sort((left, right) => { - const leftHost = getHost(left[1].url.toLowerCase()); - const rightHost = getHost(right[1].url.toLowerCase()); - if (leftHost < rightHost) - return -1; - if (leftHost > rightHost) - return 1; - return 0; + const leftHost = getHost(left[1].url); + const rightHost = getHost(right[1].url); + return window.qBittorrent.Misc.naturalSortCollator.compare(leftHost, rightHost); }); for (const [hash, tracker] of sortedList) { - trackerFilterList.appendChild(createLink(hash, getHost(tracker.url) + ' (%1)', tracker.torrents.length)); + trackerFilterList.appendChild(createLink(hash, (getHost(tracker.url) + ' (%1)'), tracker.torrents.length)); } highlightSelectedTracker(); diff --git a/src/webui/www/private/scripts/dynamicTable.js b/src/webui/www/private/scripts/dynamicTable.js index 63c9045ec..90907f921 100644 --- a/src/webui/www/private/scripts/dynamicTable.js +++ b/src/webui/www/private/scripts/dynamicTable.js @@ -57,8 +57,6 @@ window.qBittorrent.DynamicTable = (function() { }; }; - const naturalSortCollator = new Intl.Collator(undefined, { numeric: true, sensitivity: 'base' }); - const compareNumbers = (val1, val2) => { if (val1 < val2) return -1; @@ -412,7 +410,7 @@ window.qBittorrent.DynamicTable = (function() { const value2 = this.getRowValue(row2); if ((typeof(value1) === 'number') && (typeof(value2) === 'number')) return compareNumbers(value1, value2); - return naturalSortCollator.compare(value1, value2); + return window.qBittorrent.Misc.naturalSortCollator.compare(value1, value2); }; column['updateTd'] = function(td, row) { const value = this.getRowValue(row); @@ -2705,7 +2703,7 @@ window.qBittorrent.DynamicTable = (function() { const value2 = this.getRowValue(row2); if ((typeof(value1) === 'number') && (typeof(value2) === 'number')) return compareNumbers(value1, value2); - return naturalSortCollator.compare(value1, value2); + return window.qBittorrent.Misc.naturalSortCollator.compare(value1, value2); }; column['updateTd'] = function(td, row) { const value = this.getRowValue(row); @@ -2801,7 +2799,7 @@ window.qBittorrent.DynamicTable = (function() { const value2 = this.getRowValue(row2); if ((typeof(value1) === 'number') && (typeof(value2) === 'number')) return compareNumbers(value1, value2); - return naturalSortCollator.compare(value1, value2); + return window.qBittorrent.Misc.naturalSortCollator.compare(value1, value2); }; column['updateTd'] = function(td, row) { const value = this.getRowValue(row); @@ -2887,7 +2885,7 @@ window.qBittorrent.DynamicTable = (function() { const value2 = this.getRowValue(row2); if ((typeof(value1) === 'number') && (typeof(value2) === 'number')) return compareNumbers(value1, value2); - return naturalSortCollator.compare(value1, value2); + return window.qBittorrent.Misc.naturalSortCollator.compare(value1, value2); }; column['updateTd'] = function(td, row) { const value = this.getRowValue(row); @@ -2974,7 +2972,7 @@ window.qBittorrent.DynamicTable = (function() { const value2 = this.getRowValue(row2); if ((typeof(value1) === 'number') && (typeof(value2) === 'number')) return compareNumbers(value1, value2); - return naturalSortCollator.compare(value1, value2); + return window.qBittorrent.Misc.naturalSortCollator.compare(value1, value2); }; column['updateTd'] = function(td, row) { const value = this.getRowValue(row); @@ -3024,7 +3022,7 @@ window.qBittorrent.DynamicTable = (function() { const value2 = this.getRowValue(row2); if ((typeof(value1) === 'number') && (typeof(value2) === 'number')) return compareNumbers(value1, value2); - return naturalSortCollator.compare(value1, value2); + return window.qBittorrent.Misc.naturalSortCollator.compare(value1, value2); }; column['updateTd'] = function(td, row) { const value = this.getRowValue(row); diff --git a/src/webui/www/private/scripts/misc.js b/src/webui/www/private/scripts/misc.js index 618d6fec1..23e8bb520 100644 --- a/src/webui/www/private/scripts/misc.js +++ b/src/webui/www/private/scripts/misc.js @@ -41,6 +41,7 @@ window.qBittorrent.Misc = (function() { friendlyFloat: friendlyFloat, parseHtmlLinks: parseHtmlLinks, escapeHtml: escapeHtml, + naturalSortCollator: naturalSortCollator, safeTrim: safeTrim, toFixedPointString: toFixedPointString, containsAllTerms: containsAllTerms, @@ -178,6 +179,8 @@ window.qBittorrent.Misc = (function() { return escapedString; }; + const naturalSortCollator = new Intl.Collator(undefined, { numeric: true, sensitivity: 'base' }); + const safeTrim = function(value) { try { return value.trim();