1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2025-02-22 14:00:03 +00:00

changed sort and sortcs to support nsort and nsortcs

This commit is contained in:
Stephan Hradek 2013-12-29 00:15:11 +01:00
parent ac81d9d43f
commit ce8c79ecfa
2 changed files with 39 additions and 10 deletions

View File

@ -1,5 +1,5 @@
/*\ /*\
title: $:/core/modules/filters/sort.js title: $:/core/modules/filters/nsort.js
type: application/javascript type: application/javascript
module-type: filteroperator module-type: filteroperator
@ -16,6 +16,30 @@ Filter operator for sorting
Export our filter function Export our filter function
*/ */
exports.sort = function(source,operator,options) { 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; var results;
if($tw.utils.isArray(source)) { if($tw.utils.isArray(source)) {
results = source; results = source;
@ -25,8 +49,7 @@ exports.sort = function(source,operator,options) {
results.push(title); results.push(title);
}); });
} }
options.wiki.sortTiddlers(results,operator.operand,operator.prefix === "!");
return results; return results;
}; }
})(); })();

View File

@ -305,21 +305,27 @@ Sort an array of tiddler titles by a specified field
isDescending: true if the sort should be descending isDescending: true if the sort should be descending
isCaseSensitive: true if the sort should consider upper and lower case letters to be different 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; var self = this;
titles.sort(function(a,b) { titles.sort(function(a,b) {
if(sortField !== "title") { if(sortField !== "title") {
a = self.getTiddler(a).fields[sortField] || ""; a = self.getTiddler(a).fields[sortField] || "";
b = self.getTiddler(b).fields[sortField] || ""; b = self.getTiddler(b).fields[sortField] || "";
} }
if(!isCaseSensitive) { if (!isNumeric || isNaN(a) || isNaN(b)) {
if(typeof a === "string") { if(!isCaseSensitive) {
a = a.toLowerCase(); if(typeof a === "string") {
} a = a.toLowerCase();
if(typeof b === "string") { }
b = b.toLowerCase(); if(typeof b === "string") {
b = b.toLowerCase();
}
} }
} }
else {
a-= 0;
b-= 0;
}
if(a < b) { if(a < b) {
return isDescending ? +1 : -1; return isDescending ? +1 : -1;
} else { } else {