Giter Site home page Giter Site logo

Comments (6)

jashkenas avatar jashkenas commented on June 27, 2024

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");
}

from backbone.

jstotz avatar jstotz commented on June 27, 2024

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.

from backbone.

raycohen avatar raycohen commented on June 27, 2024

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

from backbone.

lintoka avatar lintoka commented on June 27, 2024

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??

from backbone.

yuchi avatar yuchi commented on June 27, 2024

@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.

from backbone.

jashkenas avatar jashkenas commented on June 27, 2024

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.

from backbone.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.