I am trying to sort a collection of models by a creation date or priority, selectable by a user.
Priority can be selected in either descending or ascending order but the models are then further sub-ordered by creation date in descending order.
Creation date can also be selected in either descending or ascending order with no further sub ordering.
Below is the code I have so far:
comparator: function(task) {
var sorter = task.get(root.sortAttr);
var subSorter = String.fromCharCode.apply(String,
_.map(task.get("created_at").split(""), function (c) {
return 0xffff - c.charCodeAt();
})
);
if(!root.sortAscending) {
console.log("desc");
return -sorter + ", " + subSorter;
}
else {
console.log("asc");
return sorter + ", " + subSorter;
}
}
This code works for both ascending and descending ordering of creation date, but will only ever show priorities in descending order. Can anyone tell me why this is the case and what I can do to fix that?
I ended up using a purely string based approach which has yielded the result I was after and offers more flexibility in terms of being able to support string based sort attributes down the track.
Where the negate string function is the same one found here and reproduced below as follows: