diff --git a/.jshintrc b/.jshintrc index 495d01b0e..e3abf5997 100644 --- a/.jshintrc +++ b/.jshintrc @@ -46,6 +46,7 @@ "combineStatistics": false, "countAndSetOffset": false, "loadLimitedResults": false, + "naturalCompare": false, "waypoint_results": false }, "browser" : true, diff --git a/resource/js/hierarchy.js b/resource/js/hierarchy.js index 2ba2656b4..ebec50e26 100644 --- a/resource/js/hierarchy.js +++ b/resource/js/hierarchy.js @@ -276,7 +276,7 @@ function getTreeConfiguration(root) { } }, 'plugins' : ['sort'], - 'sort' : function (a,b) { return this.get_text(a).toLowerCase() > this.get_text(b).toLowerCase() ? 1 : -1; } + 'sort' : function (a,b) { return naturalCompare(this.get_text(a).toLowerCase(), this.get_text(b).toLowerCase()); } }); } diff --git a/resource/js/scripts.js b/resource/js/scripts.js index 6d13728bd..79d0e8c00 100644 --- a/resource/js/scripts.js +++ b/resource/js/scripts.js @@ -4,7 +4,7 @@ * see LICENSE.txt for more information */ -/* exported getUrlParams, readCookie, createCookie, getUrlParams, debounce, updateContent, updateTopbarLang, updateTitle, updateSidebar, setLangCookie, loadLimitations, loadPage, hideCrumbs, shortenProperties, countAndSetOffset, combineStatistics, loadLimitedResults */ +/* exported getUrlParams, readCookie, createCookie, getUrlParams, debounce, updateContent, updateTopbarLang, updateTitle, updateSidebar, setLangCookie, loadLimitations, loadPage, hideCrumbs, shortenProperties, countAndSetOffset, combineStatistics, loadLimitedResults, naturalCompare */ /* * Creates a cookie value and stores it for the user. Takes the given @@ -226,3 +226,20 @@ function countAndSetOffset() { } } +// Natural sort from: http://stackoverflow.com/a/15479354/3894569 +function naturalCompare(a, b) { + var ax = [], bx = []; + + a.replace(/(\d+)|(\D+)/g, function(_, $1, $2) { ax.push([$1 || Infinity, $2 || ""]); }); + b.replace(/(\d+)|(\D+)/g, function(_, $1, $2) { bx.push([$1 || Infinity, $2 || ""]); }); + + while(ax.length && bx.length) { + var an = ax.shift(); + var bn = bx.shift(); + var nn = (an[0] - bn[0]) || an[1].localeCompare(bn[1]); + if(nn) return nn; + } + + return ax.length - bx.length; +} +