changed suffix syntax to prefix

This commit is contained in:
KungD
2012-08-28 21:21:58 +02:00
parent b9e5c1e69e
commit 8fafad1208
2 changed files with 5 additions and 5 deletions

View File

@@ -743,8 +743,8 @@
if (!comparator) throw new Error('Cannot sort a set without a comparator');
if (typeof comparator === "string"){
this.models.sort(function(o1,o2){
var attrName = comparator.replace(/\+|\-$/,"");
return (o2.get(attrName) > o1.get(attrName) ? -1 : 1)*(comparator[comparator.length-1] === "-" ? -1 : 1);
var attrName = comparator.replace(/^\+|\-/,"");
return (o2.get(attrName) > o1.get(attrName) ? -1 : 1)*(comparator[0] === "-" ? -1 : 1);
});
} else {
var boundComparator = _.bind(comparator, this);

View File

@@ -46,15 +46,15 @@ $(document).ready(function() {
equal(col.last(), a, "a should be last");
equal(col.length, 4);
// tests with string comparator
col.comparator = "label+"; // suffix "+" means ascending sort
col.comparator = "+label"; // prefix "+" means ascending sort
col.sort();
equal(col.first(), a, "a should be first");
equal(col.last(), d, "d should be last");
col.comparator = "label-"; // suffix "-" means descending sort
col.comparator = "-label"; // prefix "-" means descending sort
col.sort();
equal(col.first(), d, "d should be first");
equal(col.last(), a, "a should be last");
col.comparator = "label"; // default to ascending
col.comparator = "label"; // no prefix defaults to ascending
col.sort();
equal(col.first(), a, "a should be first");
equal(col.last(), d, "d should be last");