-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
Make the comparator method behave like Array.sort() #9
Comments
I've added a note in the documentation that explains the rationale for the current behavior of You can certainly use it to do arbitrary comparison functions that take multiple properties into account:
|
How would one go about sorting by one field ascending and by another descending? In simple cases I agree the sortBy style is more convenient but it's somewhat limited. Perhaps it could be made to switch between sort mechanisms automatically based on the arity of the comparator function. Use the standard JavaScript sort API if passed a comparator function that expects 2 arguments, otherwise use sortBy. Maybe this could be implemented in Underscore to make it transparent to Backbone. |
It would be really nice if there were a more elegant way to reverse sort by a string field. Workarounds for this are hard to swallow... http://stackoverflow.com/questions/5636812/sorting-strings-in-reverse-order-with-backbone-js |
comparator: function() { is't this solution expensive when it comes to some values like timestamps?? |
@kmalakoff has done some good work into this, in the |
This ticket is an oldie but a goodie. I've just pushed a commit to master that will allow you to specify a comparator with either |
congrats to backbone.js, looks very promising.
One point of critism tough: the Backbone.Collection.Comparator method looks a bit weird to me. I would appreciate a comparator method defined in Backbone.Model that works like Javascript's array sort: it gets 2 model instances a and b for comparison and returns -1 if a > b, 0 if a == b or 1 if a < b.
This would allow more elaborate comparison functions that e.g. take multiple properties into account.
The text was updated successfully, but these errors were encountered: