mirror of
https://github.com/jashkenas/backbone.git
synced 2026-04-30 03:00:06 -04:00
changed suffix syntax to prefix
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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");
|
||||
|
||||
Reference in New Issue
Block a user