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

Make the comparator method behave like Array.sort() #9

Closed
fbuchinger opened this issue Oct 14, 2010 · 6 comments
Closed

Make the comparator method behave like Array.sort() #9

fbuchinger opened this issue Oct 14, 2010 · 6 comments

Comments

@fbuchinger
Copy link

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.

@jashkenas
Copy link
Owner

I've added a note in the documentation that explains the rationale for the current behavior of comparator -- I think it's a much nicer API than a 0, 1, -1 sort, and is commonly referred to as sortBy.

You can certainly use it to do arbitrary comparison functions that take multiple properties into account:

comparator: function() {
  return this.get("last_name") + ", " + this.get("first_name");
}

comparator: function() {
  return this.get("pos_y") * 1000 + this.get("pos_x");
}

@jstotz
Copy link

jstotz commented Feb 7, 2011

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.

@raycohen
Copy link

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

@lintoka
Copy link

lintoka commented Dec 6, 2011

comparator: function() {
return this.get("pos_y") * 1000 + this.get("pos_x");
}

is't this solution expensive when it comes to some values like timestamps??

@jashkenas jashkenas reopened this Dec 6, 2011
@yuchi
Copy link

yuchi commented Dec 6, 2011

@kmalakoff has done some good work into this, in the _.compare method. He has probably gone too far, but IMHO it does offer some good features.

@jashkenas
Copy link
Owner

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 sort() or sortBy() semantics.

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

No branches or pull requests

6 participants