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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:32:37+00:00 2026-06-17T22:32:37+00:00

Ok so, I have a collection filled with objects of a certain model, this

  • 0

Ok so, I have a collection filled with objects of a certain model, this model contains a date and a priority, I want it to be sorted by date, then by the priority, I currently have it implemented this way:

App.Collections.Tasks = Backbone.Collection.extend({
model: App.Models.Task,
comparator: function(model) 
{
  return model.get("date") + model.get("priority");
}
});

This is my output:

Get to the party  Edit Delete 1 Fri Feb 1
Get to the party  Edit Delete 2 Mon Jan 28
Go to the store  Edit Delete 4 Mon Jan 28
Go to the mall  Edit Delete 3 Tue Jan 29
Get to the party  Edit Delete 3 Tue Jan 29
Get to the party  Edit Delete 5 Tue Jan 29
Get to the party  Edit Delete 2 Wed Jan 30
Get to work  Edit Delete 5 Wed Jan 30

I want earlier dates to be on top at all times so the february date should be last, how could I achieve this?

Thanks!

  • 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-06-17T22:32:37+00:00Added an answer on June 17, 2026 at 10:32 pm

    You can do it like that:

    comparator: function(model1, model2) {
        return (model1.get("date") + model1.get("priority")) -
            (model2.get("date") + model2.get("priority"));
    }
    

    Documentation claims, that returned value should be -1, 0 or 1, but that wasn’t mandatory earlier and any value could do the trick.

    EDIT

    comparator: function(model1, model2) {
        var comp = (model1.get("date") + model1.get("priority")) -
            (model2.get("date") + model2.get("priority"));
        if (comp < 0) {
            return -1;
        } else if (comp > 0) {
            return 1;
        } else {
            return 0;
        }
    }
    

    Now code precisely follows specification mentioned. If this didn’t solve the problem, then the problem is not in comparator.

    EDIT

    Silly me. I assumed that you are storing a unix time in date, but apparently you don’t. Subtracting strings gives NaN, so no sorting is made indeed. So all you need to do now is to cast date to unix time format (or at least something similar), then it should be working.

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

Sidebar

Related Questions

I have a collection that is filled with weak references to objects in an
I have Collection List<Car> . How to compare each item from this collection with
I have Class and Student objects. Both have collection of another as property. Which
I have a scenario where I have a collection of objects bound to a
I have a collection of objects we'll call People each comprised of Name, ID
I have a collection of millions of instances of this simple object: { Org:
If I have a collection object which is empty but needs to be filled
I have a collection of cookies filled with Browserhawk information in masterpage.master.vb such as;
I have a ListView that contains a large collection of rows with textboxes that
Lets say i have an Array collection, filled with some elements. If i say

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.