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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:10:36+00:00 2026-06-01T20:10:36+00:00

Say I have an array of a few objects: var array = [{id: 1,

  • 0

Say I have an array of a few objects:

var array = [{id: 1, date: Mar 12 2012 10:00:00 AM}, {id: 2, date: Mar 8 2012 08:00:00 AM}];

How can I sort this array by the date element in order from the date closest to the current date and time down? Keep in mind that the array may have many objects, but for the sake of simplicity I used 2.

Would I use the sort function and a custom comparator?

  • 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-01T20:10:37+00:00Added an answer on June 1, 2026 at 8:10 pm

    Simplest Answer

    array.sort(function(a,b){
      // Turn your strings into dates, and then subtract them
      // to get a value that is either negative, positive, or zero.
      return new Date(b.date) - new Date(a.date);
    });
    

    More Generic Answer

    array.sort(function(o1,o2){
      if (sort_o1_before_o2)    return -1;
      else if(sort_o1_after_o2) return  1;
      else                      return  0;
    });
    

    Or more tersely:

    array.sort(function(o1,o2){
      return sort_o1_before_o2 ? -1 : sort_o1_after_o2 ? 1 : 0;
    });
    

    Generic, Powerful Answer

    Define a custom non-enumerable sortBy function using a Schwartzian transform on all arrays :

    (function(){
      if (typeof Object.defineProperty === 'function'){
        try{Object.defineProperty(Array.prototype,'sortBy',{value:sb}); }catch(e){}
      }
      if (!Array.prototype.sortBy) Array.prototype.sortBy = sb;
    
      function sb(f){
        for (var i=this.length;i;){
          var o = this[--i];
          this[i] = [].concat(f.call(o,o,i),o);
        }
        this.sort(function(a,b){
          for (var i=0,len=a.length;i<len;++i){
            if (a[i]!=b[i]) return a[i]<b[i]?-1:1;
          }
          return 0;
        });
        for (var i=this.length;i;){
          this[--i]=this[i][this[i].length-1];
        }
        return this;
      }
    })();
    

    Use it like so:

    array.sortBy(function(o){ return o.date });
    

    If your date is not directly comparable, make a comparable date out of it, e.g.

    array.sortBy(function(o){ return new Date( o.date ) });
    

    You can also use this to sort by multiple criteria if you return an array of values:

    // Sort by date, then score (reversed), then name
    array.sortBy(function(o){ return [ o.date, -o.score, o.name ] };
    

    See http://phrogz.net/JS/Array.prototype.sortBy.js for more details.

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

Sidebar

Related Questions

Let's say I have this: var blockedTile = new Array(118, 67, 190, 43, 135,
I'm progamming in javascript/jQuery. Say, I have an array with includes a few number
Say I have an array like this: [white, red, blue, red, white, green, red,
Say I have a class with a private dispatch table. $this->dispatch = array( 1
Let's say I have array of bytes: byte[] arr = new byte[] { 0,
Say I have an array of table DDL queries and I want to get
Say I have an array of key/value pairs in PHP: array( 'foo' => 'bar',
Say I have an array of arbitrary size holding single characters. I want to
Say I have an array with the following members: car_porsche car_mercedes car_toyota motorcycle_suzuki motorcycle_honda
Say I have an array of number a <- c(1,2,3,6,7,8,9,10,20) if there a way

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.