From ce8c79ecfa3071cfe6f5b555c9b7fc4ae1a3ae72 Mon Sep 17 00:00:00 2001 From: Stephan Hradek Date: Sun, 29 Dec 2013 00:15:11 +0100 Subject: [PATCH 1/4] changed sort and sortcs to support nsort and nsortcs --- core/modules/filters/sort.js | 29 ++++++++++++++++++++++++++--- core/modules/wiki.js | 20 +++++++++++++------- 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/core/modules/filters/sort.js b/core/modules/filters/sort.js index 538ac0ae2..efa3c05bb 100644 --- a/core/modules/filters/sort.js +++ b/core/modules/filters/sort.js @@ -1,5 +1,5 @@ /*\ -title: $:/core/modules/filters/sort.js +title: $:/core/modules/filters/nsort.js type: application/javascript module-type: filteroperator @@ -16,6 +16,30 @@ Filter operator for sorting Export our filter function */ exports.sort = function(source,operator,options) { + var results = prepare_results(source); + options.wiki.sortTiddlers(results,operator.operand,operator.prefix === "!",false,false); + return results; +}; + +exports.nsort = function(source,operator,options) { + var results = prepare_results(source); + options.wiki.sortTiddlers(results,operator.operand,operator.prefix === "!",false,true); + return results; +}; + +exports.sortcs = function(source,operator,options) { + var results = prepare_results(source); + options.wiki.sortTiddlers(results,operator.operand,operator.prefix === "!",true,false); + return results; +}; + +exports.nsortcs = function(source,operator,options) { + var results = prepare_results(source); + options.wiki.sortTiddlers(results,operator.operand,operator.prefix === "!",true,true); + return results; +}; + +var prepare_results = function (source) { var results; if($tw.utils.isArray(source)) { results = source; @@ -25,8 +49,7 @@ exports.sort = function(source,operator,options) { results.push(title); }); } - options.wiki.sortTiddlers(results,operator.operand,operator.prefix === "!"); return results; -}; +} })(); diff --git a/core/modules/wiki.js b/core/modules/wiki.js index cf9f74973..80b8fbfe0 100755 --- a/core/modules/wiki.js +++ b/core/modules/wiki.js @@ -305,21 +305,27 @@ Sort an array of tiddler titles by a specified field isDescending: true if the sort should be descending isCaseSensitive: true if the sort should consider upper and lower case letters to be different */ -exports.sortTiddlers = function(titles,sortField,isDescending,isCaseSensitive) { +exports.sortTiddlers = function(titles,sortField,isDescending,isCaseSensitive,isNumeric) { var self = this; titles.sort(function(a,b) { if(sortField !== "title") { a = self.getTiddler(a).fields[sortField] || ""; b = self.getTiddler(b).fields[sortField] || ""; } - if(!isCaseSensitive) { - if(typeof a === "string") { - a = a.toLowerCase(); - } - if(typeof b === "string") { - b = b.toLowerCase(); + if (!isNumeric || isNaN(a) || isNaN(b)) { + if(!isCaseSensitive) { + if(typeof a === "string") { + a = a.toLowerCase(); + } + if(typeof b === "string") { + b = b.toLowerCase(); + } } } + else { + a-= 0; + b-= 0; + } if(a < b) { return isDescending ? +1 : -1; } else { From cee1bc0863885d545992e875ab355fe75f9bc5b3 Mon Sep 17 00:00:00 2001 From: Stephan Hradek Date: Sun, 29 Dec 2013 00:19:51 +0100 Subject: [PATCH 2/4] small typofix --- core/modules/filters/sort.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/modules/filters/sort.js b/core/modules/filters/sort.js index efa3c05bb..26a240886 100644 --- a/core/modules/filters/sort.js +++ b/core/modules/filters/sort.js @@ -1,5 +1,5 @@ /*\ -title: $:/core/modules/filters/nsort.js +title: $:/core/modules/filters/sort.js type: application/javascript module-type: filteroperator From 38142d2b19464cda08e10bc0fcdbb6cc82809c7c Mon Sep 17 00:00:00 2001 From: Stephan Hradek Date: Sun, 29 Dec 2013 22:32:23 +0100 Subject: [PATCH 3/4] =?UTF-8?q?Missed=20that=20I=20need=20to=20explicitly?= =?UTF-8?q?=20need=20to=20git=20rm=20=E2=80=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/modules/filters/sortcs.js | 32 -------------------------------- 1 file changed, 32 deletions(-) delete mode 100644 core/modules/filters/sortcs.js diff --git a/core/modules/filters/sortcs.js b/core/modules/filters/sortcs.js deleted file mode 100644 index 74117aa42..000000000 --- a/core/modules/filters/sortcs.js +++ /dev/null @@ -1,32 +0,0 @@ -/*\ -title: $:/core/modules/filters/sortcs.js -type: application/javascript -module-type: filteroperator - -Filter operator for case-sensitive sorting - -\*/ -(function(){ - -/*jslint node: true, browser: true */ -/*global $tw: false */ -"use strict"; - -/* -Export our filter function -*/ -exports.sortcs = function(source,operator,options) { - var results; - if($tw.utils.isArray(source)) { - results = source; - } else { - results = []; - $tw.utils.each(source,function(element,title) { - results.push(title); - }); - } - options.wiki.sortTiddlers(results,operator.operand,operator.prefix === "!",true); - return results; -}; - -})(); From c9b319c41cbd6150d3e6ff468b2af79ebdce58f2 Mon Sep 17 00:00:00 2001 From: Stephan Hradek Date: Wed, 1 Jan 2014 23:59:09 +0100 Subject: [PATCH 4/4] nsort - adapted documentation --- editions/tw5.com/tiddlers/concepts/TiddlerFilters.tid | 2 ++ 1 file changed, 2 insertions(+) diff --git a/editions/tw5.com/tiddlers/concepts/TiddlerFilters.tid b/editions/tw5.com/tiddlers/concepts/TiddlerFilters.tid index 5446537b3..bcc02a47e 100644 --- a/editions/tw5.com/tiddlers/concepts/TiddlerFilters.tid +++ b/editions/tw5.com/tiddlers/concepts/TiddlerFilters.tid @@ -36,6 +36,8 @@ A filter string consists of one or more runs of filter operators that each look * ''has'': tests whether a tiddler has the field specified in the operand * ''sort'': sorts the tiddlers by the field specified in the operand * ''sortcs'': a case sensitive sort of the current tiddlers by the field specified in the operand +* ''nsort'': sorts the tiddlers numerically by the field specified in the operand +* ''nsortcs'': a case sensitive (for the non-numerical elements) numerical sort of the current tiddlers by the field specified in the operand * ''prefix'': tests whether a tiddlers title starts with the prefix specified in the operand * ''limit'': limits the number of subresults to the integer specified in the operand * ''tag'': tests whether a given tag is (`[tag[mytag]]`) or is not (`[!tag[mytag]]`) present on the tiddler