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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:37:11+00:00 2026-05-30T18:37:11+00:00

I have a backbone collection which has a bunch of models with date attributes

  • 0

I have a backbone collection which has a bunch of models with date attributes associated with to them. I want to sort them based on their dates. So the latest dates first and so on.
Whats the best way to go around this.

The dates are formatted like this, a basic date object. Date {Mon Mar 05 2012 23:30:00 GMT-0500 (EST)}

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-05-30T18:37:12+00:00Added an answer on May 30, 2026 at 6:37 pm

    You have Date objects so you can use getTime to convert them to numbers and then negate those numbers to get the most-recent dates first. If you want to keep your collection sorted then a comparator like this:

    C = Backbone.Collection.extend({
        //...
        comparator: function(m) {
            return -m.get('date').getTime();
        }
    });
    

    will do the trick. Demo (open your console please): http://jsfiddle.net/ambiguous/htcyh/

    Backbone collections also include Underscore’s sortBy so you could do a one-time sort:

    var sorted = c.sortBy(function(m) { return -m.get('date').getTime() });
    

    Demo: http://jsfiddle.net/ambiguous/FF5FP/

    Or you could use toArray to get a normal JavaScript array and use the standard sort without using getTime:

    var sorted = c.toArray().sort(function(a, b) {
        a = a.get('date');
        b = b.get('date');
        if(a > b)
            return -1;
        if(a < b)
            return 1;
        return 0;
    });
    

    Demo: http://jsfiddle.net/ambiguous/QRmJ4/

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

Sidebar

Related Questions

A Backbone app which I'm developing has a collection and a model, and associated
I have a backbone.js collection with male and female models. In my interface I
We're designing a backbone application, in which each server-side collection has the potential to
I have a post which has many comments, and I want to display the
I have a backbone model which contains an array which I am updating with
I have a backbone application which, upon load, needs to fetch data from four
As in the backbone-todolist example,I have a collection of elements. I made 2 views,
I have a situation which has to be simple to solve, I would guess.
I have the following Backbone.js model and collection: // Model lr.Event = Backbone.Model.extend({}); //
I have implemented a simple close() method for all the Backbone views which disposes

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.