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 7421391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:24:00+00:00 2026-05-29T08:24:00+00:00

Imagine a Model/Collection like: var AModel = Backbone.Model.extend({ defaults: { a: ‘a string’, b:

  • 0

Imagine a Model/Collection like:

var AModel = Backbone.Model.extend({
    defaults: {
        a: 'a string',
        b: 'another string',
        c: 'yet another string'
    }
});

var ACollection = Backbone.Collection.extend({
    model: AModel,
    comparator: function(amodel) {
        ...
    }
});

How could I write a comparator to implement a multi-level sort? I’d like to sort by AModel‘s a attribute, then by its b attribute, and then by its c attribute.

I’ve hacked together a comparator that looks something like this, but I want to know if there is a better/smarter way?

comparator: function(amodel) {
    var s = '',
        assumed_max_length_of_any_attribute = 30;

    s += amodel.get('a');
    while (s.length < assumed_max_length_of_any_attribute) {
        s += ' ';
    }

    s += amodel.get('b');
    while (s.length < assumed_max_length_of_any_attribute) {
        s += ' ';
    }

    s += amodel.get('c');
    while (s.length < assumed_max_length_of_any_attribute) {
        s += ' ';
    }

    return s;
}

Then, s becomes properly padded with spaces and should be in “lexical” order with multiple levels. But it all feels very gross compared to the beauty of python’s stable multi-level sort (if somehow the above had analogous equivalents in python):

collection.sort(key=lambda x: x.get('c'))
collection.sort(key=lambda x: x.get('b'))
collection.sort(key=lambda x: x.get('a'))

Is there a better way?

  • 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-29T08:24:01+00:00Added an answer on May 29, 2026 at 8:24 am

    The backbone documentation says:

    Comparator function can be defined as either a sortBy (pass a function
    that takes a single argument), or as a sort (pass a comparator
    function that expects two arguments).

    http://documentcloud.github.com/backbone/#Collection-comparator

    You could use the second way and implement your comparison based on two elements given.

    Maybe something like this:

    helper: function (c1, c2) {
        if (c1 < c2) return -1;
        if (c1 > c2) return +1;
        return 0;
    }
    comparator: function (model1, model2) {
        return _.reduce(["c", "b", "a"], function (acc, comp) {
            return acc !== 0 ? acc : this.helper(model1[comp], model2[comp])
        }, 0);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Imagine a code like this one: @RequestMapping(value=/users, method=RequestMethod.GET) public String list(Model model) { ...
Imagine a model like this: class CFile(models.Model): filepath = models.FileField(upload_to=...) collection = models.ForeignKey(FileCollection,null=True) ...
Imagine a model structure as follows: models/cross_sell_promotion.rb class CrossSellPromotion < Promotion has_and_belongs_to_many :afflicted_products, :join_table
I have a problem. Imagine this data model: [Person] table has: PersonId, Name1 [Tag]
Imagine an object you are working with has a collection of other objects associated
Imagine you have this model: class Category(models.Model): node_id = models.IntegerField(primary_key = True) type_id =
Imagine I have a model called Course and each course has_many Modules . However
Imagine I've got a data object that makes sense in an OO model, but
Hi (sorry for my bad english :p) Imagine these models : class Fruit(models.Model): #
Im playing a little bit with heavy-client app. Imagine I have this model: class

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.