Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reverse sort order of models in a collection #314

Closed
rubiii opened this issue Apr 11, 2011 · 12 comments
Closed

reverse sort order of models in a collection #314

rubiii opened this issue Apr 11, 2011 · 12 comments
Labels

Comments

@rubiii
Copy link

rubiii commented Apr 11, 2011

hey,

what's the best way to reverse the sort order a collection? my comparator sorts all models by date,
but i would need them in reverse order. using toArray() and reverse() seems pretty ugly.

given i'm not missing something important here, i'd vote for a backbone model attribute to set the
sort order along with the comparator.

cheers,
daniel

@jashkenas
Copy link
Owner

The best way to reverse the sort of the collection is to return a negative value from your comparison criterion.

comparator : function(model) {
  return -model.get('date');
}

@kkaefer
Copy link

kkaefer commented Aug 10, 2011

How is this supposed to work when sorting alphabetically on a string value?

@inf0rmer
Copy link

If all you have are string values, then maybe convert to a Date before?
I had the same problem and this solved it.

comparator : function(model) {
  return -new Date(model.get('date'));
}

@kkaefer
Copy link

kkaefer commented Jan 12, 2012

That only works for dates (which are converted into numbers when you prefix them with -), but not for arbitrary strings that you want to sort alphabetically.

@inf0rmer
Copy link

You're of course right, I assumed you were still talking about dates (like the OP), but that your date was a convertible string. My mistake then :).

@jashkenas
Copy link
Owner

The next version of Backbone (and the current master) will support sort orderings for comparators (instead of just sortBy), so you'll be able to define it in the usual way.

comparator: function(a, b) {
  ...
}

@elwayman02
Copy link

What do you do if you have multiple use-cases for the same collection, some of which should be shown in one order, and the other in the reverse? Your comparator and sort implementations can only pick one direction or the other, there doesn't seem to be a Collection.reverse() to be able to quickly switch sort directions (ascending vs descending).

@philfreo
Copy link
Contributor

@elwayman02 You can just maintain 2 collection instances (Collection#clone may help)

@elwayman02
Copy link

That's not particularly useful...take for example a situation where the collection represents a grid of data that I will be repeatedly sorting in multiple directions. It doesn't make sense to try and maintain two different sources of data and switch between them. There should be one data object that we manipulate as needed.

@caseywebdev
Copy link
Collaborator

I've stored multiple comparators on a collection, and swapped them out as needed.

var Collection = Backbone.Collection.extend({
  comparators: {
    a: function () {},
    b: function () {}
  },

  setComparator: function (key) {
    this.comparator = this.comparators[key];
    return this;
  }
});
var collection = new Collection();
collection.setComparator('a').sort();
collection.setComparator('b').sort();

@elwayman02
Copy link

That's a better workaround, although I still don't understand why a workaround is necessary instead of backbone simply supporting a reverse() method.

@caseywebdev
Copy link
Collaborator

You could also do

collection.set(collection.models.reverse(), {sort: false});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants