Am Samstag, 28. Dezember 2013 01:19:52 UTC+1 schrieb Lorenzo:
>
> Oh, now I see the problem: you consider "0015" as "15", but I think at it
> as the string "0015". This is why I think it's preferable to specify a type
> for the list and not to work in mixed mode
>
Create this tiddler and you have an nsort function which is exactly the
sort function from above. But as long as you don't mix strings with
numbers… So for sorting strings take "sort" for sorting pure number lists
take "nsort" but be prepared to get the same result as before when having
mixed lists.
/*\
title: $:/core/modules/filters/nsort.js
type: application/javascript
module-type: filteroperator
Filter operator for sorting
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
/*
Export our filter function
*/
exports.nsort = 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);
});
}
nsort(results,operator.operand,operator.prefix === "!");
return results;
};
})();
var nsort = function(titles,sortField,isDescending,isCaseSensitive) {
var wiki = $tw.wiki;
titles.sort(function(a,b) {
if(sortField !== "title") {
a = wiki.getTiddler(a).fields[sortField] || "";
b = wiki.getTiddler(b).fields[sortField] || "";
}
if(isNaN(a) || isNaN(b)) {
if(!isCaseSensitive) {
if(typeof a === "string") {
a = a.toLowerCase();
}
if(typeof b === "string") {
b = b.toLowerCase();
}
}
}
else {
a = a-0;
b = b-0;
}
if(a < b) {
return isDescending ? +1 : -1;
} else {
if(a > b) {
return isDescending ? -1 : +1;
} else {
return 0;
}
}
});
};
--
You received this message because you are subscribed to the Google Groups
"TiddlyWiki" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/tiddlywiki.
For more options, visit https://groups.google.com/groups/opt_out.