Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7687893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:48:06+00:00 2026-05-31T19:48:06+00:00

I can successfully do this: App.SomeCollection = Backbone.Collection.extend({ comparator: function( collection ){ return( collection.get(

  • 0

I can successfully do this:

App.SomeCollection = Backbone.Collection.extend({
  comparator: function( collection ){
    return( collection.get( 'lastName' ) );
  }
});

Which is nice if I want to have a collection that is only sorted by ‘lastName’. But I need to have this sorting done dynamically. Sometimes, I’ll need to sort by, say, ‘firstName’ instead.

My utter failures include:

I tried passing an extra variable specifying the variable to sort() on. That did not work. I also tried sortBy(), which did not work either. I tried passing my own function to sort(), but this did not work either. Passing a user-defined function to sortBy() only to have the result not have an each method, defeating the point of having a newly sorted backbone collection.

Can someone provide a practical example of sorting by a variable that is not hard coded into the comparator function? Or any hack you have that works? If not, a working sortBy() call?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T19:48:08+00:00Added an answer on May 31, 2026 at 7:48 pm

    Interesting question. I would try a variant on the strategy pattern here. You could create a hash of sorting functions, then set comparator based on the selected member of the hash:

    App.SomeCollection = Backbone.Collection.extend({
        comparator: strategies[selectedStrategy],
        strategies: {
            firstName: function () { /* first name sorting implementation here */ }, 
            lastName: function () { /* last name sorting implementation here */ },
        },
        selectedStrategy: "firstName"
    });
    

    Then you could change your sorting strategy on the fly by updating the value of the selectedStrategy property.

    EDIT: I realized after I went to bed 🙂 that this wouldn’t quite work as I wrote it above, because we’re passing an object literal to Collection.extend. The comparator property will be evaluated once, when the object is created, so it won’t change on the fly unless forced to do so. There is probably a cleaner way to do this, but this demonstrates switching the comparator functions on the fly:

    var SomeCollection = Backbone.Collection.extend({
        comparator: function (property) {
            return selectedStrategy.apply(myModel.get(property));
        },
        strategies: {
            firstName: function (person) { return person.get("firstName"); }, 
            lastName: function (person) { return person.get("lastName"); },
        },
        changeSort: function (sortProperty) {
            this.comparator = this.strategies[sortProperty];
        },
        initialize: function () {
            this.changeSort("lastName");
            console.log(this.comparator);
            this.changeSort("firstName");
            console.log(this.comparator);        
        }                                                                                        
    });
    
    var myCollection = new SomeCollection;
    

    Here’s a jsFiddle that demonstrates this.

    The root of all of your problems, I think, is that properties on JavaScript object literals are evaluated immediately when the object is created, so you have to overwrite the property if you want to change it. If you try to write some kind of switching into the property itself it’ll get set to an initial value and stay there.

    Here’s a good blog post that discusses this in a slightly different context.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this app (contacts style) in which you can add instances to a
I have an HBITMAP containing alpha channel data. I can successfully render this using
Can anyone explain how I can successfully get my processes communicating? I find the
I can successfully push only next view in my iPhone app. However, cause the
In my Silverlight RIA app I can successfully inject an ExceptionHelper class via constructor
In my iPhone app, I can successfully change language inside the app's Settings (just
I have set up Actions for my facebook open graph app and can successfully
I can successfully initiate a phone call within my app using the following: [[UIApplication
I can successfully send C2DM messages to my android emulator in Local App Engine
I can successfully connect to MySQL from a DOS prompt, but when I try

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.