From ad62d9a083d3b2509859fc7dbc57a1de1034cb35 Mon Sep 17 00:00:00 2001 From: Tobias Beer Date: Fri, 23 Jan 2015 12:06:32 +0100 Subject: [PATCH 1/4] first attempt at fixing wrong sort order in nsort and nsortcs not working yet, no idea why --- core/modules/wiki.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/core/modules/wiki.js b/core/modules/wiki.js index 75245ac17..0a0cfaa85 100755 --- a/core/modules/wiki.js +++ b/core/modules/wiki.js @@ -314,7 +314,7 @@ Sort an array of tiddler titles by a specified field isCaseSensitive: true if the sort should consider upper and lower case letters to be different */ exports.sortTiddlers = function(titles,sortField,isDescending,isCaseSensitive,isNumeric) { - var self = this; + var x,y,self = this; titles.sort(function(a,b) { if(sortField !== "title") { var tiddlerA = self.getTiddler(a), @@ -330,10 +330,13 @@ exports.sortTiddlers = function(titles,sortField,isDescending,isCaseSensitive,is b = ""; } } - if(isNumeric) { - a = Number(a); - b = Number(b); - return isDescending ? b - a : a - b; + x = Number(a); + y = Number(b); + if(isNumeric && (!isNaN(x) || !isNaN(y))) { + return + isNaN(x) && !isNaN(y) ? (isDescending ? -1 : 1) : + !isNaN(x) && isNaN(y) ? (isDescending ? 1 : -1) : + isDescending ? x - y : y - x; } else if($tw.utils.isDate(a) && $tw.utils.isDate(b)) { return isDescending ? b - a : a - b; } else { From 122e16d39cc84d15757819abce05810220431394 Mon Sep 17 00:00:00 2001 From: Tobias Beer Date: Fri, 23 Jan 2015 12:24:47 +0100 Subject: [PATCH 2/4] putting the variables in the right scope --- core/modules/wiki.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/core/modules/wiki.js b/core/modules/wiki.js index 0a0cfaa85..6176ab86c 100755 --- a/core/modules/wiki.js +++ b/core/modules/wiki.js @@ -314,10 +314,11 @@ Sort an array of tiddler titles by a specified field isCaseSensitive: true if the sort should consider upper and lower case letters to be different */ exports.sortTiddlers = function(titles,sortField,isDescending,isCaseSensitive,isNumeric) { - var x,y,self = this; + var self = this; titles.sort(function(a,b) { if(sortField !== "title") { - var tiddlerA = self.getTiddler(a), + var x,y, + tiddlerA = self.getTiddler(a), tiddlerB = self.getTiddler(b); if(tiddlerA) { a = tiddlerA.fields[sortField] || ""; From 8a3991a619ac1c71792fa8588c5bf36381c93b3d Mon Sep 17 00:00:00 2001 From: Tobias Beer Date: Fri, 23 Jan 2015 14:57:04 +0100 Subject: [PATCH 3/4] extracted number comparision function and added missing brackets --- core/modules/wiki.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/core/modules/wiki.js b/core/modules/wiki.js index 6176ab86c..ad1b94212 100755 --- a/core/modules/wiki.js +++ b/core/modules/wiki.js @@ -316,9 +316,16 @@ Sort an array of tiddler titles by a specified field exports.sortTiddlers = function(titles,sortField,isDescending,isCaseSensitive,isNumeric) { var self = this; titles.sort(function(a,b) { + var x,y, + compareNumbers = function(x,y) { + var result = + isNaN(x) && !isNaN(y) ? (isDescending ? -1 : 1) : + !isNaN(x) && isNaN(y) ? (isDescending ? 1 : -1) : + (isDescending ? y - x : x - y); + return result; + }; if(sortField !== "title") { - var x,y, - tiddlerA = self.getTiddler(a), + var tiddlerA = self.getTiddler(a), tiddlerB = self.getTiddler(b); if(tiddlerA) { a = tiddlerA.fields[sortField] || ""; @@ -334,10 +341,7 @@ exports.sortTiddlers = function(titles,sortField,isDescending,isCaseSensitive,is x = Number(a); y = Number(b); if(isNumeric && (!isNaN(x) || !isNaN(y))) { - return - isNaN(x) && !isNaN(y) ? (isDescending ? -1 : 1) : - !isNaN(x) && isNaN(y) ? (isDescending ? 1 : -1) : - isDescending ? x - y : y - x; + return compareNumbers(x,y); } else if($tw.utils.isDate(a) && $tw.utils.isDate(b)) { return isDescending ? b - a : a - b; } else { From e9bdf6f542631398c796f4ee759d99400784f245 Mon Sep 17 00:00:00 2001 From: Tobias Beer Date: Mon, 26 Jan 2015 19:58:00 +0100 Subject: [PATCH 4/4] trying to show the conditional nicely aligned --- core/modules/wiki.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/modules/wiki.js b/core/modules/wiki.js index ad1b94212..ec56d97b7 100755 --- a/core/modules/wiki.js +++ b/core/modules/wiki.js @@ -321,7 +321,7 @@ exports.sortTiddlers = function(titles,sortField,isDescending,isCaseSensitive,is var result = isNaN(x) && !isNaN(y) ? (isDescending ? -1 : 1) : !isNaN(x) && isNaN(y) ? (isDescending ? 1 : -1) : - (isDescending ? y - x : x - y); + (isDescending ? y - x : x - y); return result; }; if(sortField !== "title") {