Reputation: 133
so, this works:
comparator: function(model) {
if(this.activeSort == "time_created") {
return -(Date.parse(model.get('time_created')) || 1);
} else {
var sort = [
model.get('title'),
model.get('category'),
];
return sort;
}
}
but if I want to sort by "time_created" and the other parameters (title, category), the sorting no longer works. I can't figure it out!
Basically, I want this to work:
comparator: function(model) {
if(this.activeSort == "time_created") {
var sort = [
// date descending
-(Date.parse(model.get('time_created')) || 1),
model.get('title'),
model.get('category'),
];
return sort;
} else {
var sort = [
model.get('title'),
model.get('category'),
];
return sort;
}
}
Upvotes: 2
Views: 151
Reputation: 413826
You really should be using a two-argument comparator. Returning an array is not a good way to make a single-argument comparator work as a multi-attribute comparator, mostly because it simply doesn't work reliably (as you've found out).
So:
comparator: function(o1, o2) {
var v1, v2;
if (this.activeSort == "time_created") {
v1 = Date.parse(o1.get("time_created")) || 1;
v2 = Date.parse(o2.get("time_created")) || 1;
if (v1 > v2)
return -1;
if (v2 > v1)
return 1;
}
v1 = o1.get("title");
v2 = o2.get("title");
if (v1 < v2)
return -1;
if (v2 < v1)
return 1;
v1 = o1.get("category");
v2 = o2.get("category");
if (v1 < v2)
return -1;
if (v2 < v1)
return 1;
return 0;
}
Upvotes: 2