Skip to content

Commit

Permalink
fixed the hierarchy sort incorrectly sorting notations with numeric c…
Browse files Browse the repository at this point in the history
…omponents (eg. KE11 appeared before KE4), related to NatLibFi#265
  • Loading branch information
henriyli committed Sep 24, 2015
1 parent 276bda6 commit 17b7d00
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
1 change: 1 addition & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"combineStatistics": false,
"countAndSetOffset": false,
"loadLimitedResults": false,
"naturalCompare": false,
"waypoint_results": false
},
"browser" : true,
Expand Down
2 changes: 1 addition & 1 deletion resource/js/hierarchy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()); }
});
}

19 changes: 18 additions & 1 deletion resource/js/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

0 comments on commit 17b7d00

Please sign in to comment.