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

  • Home
  • SEARCH
  • 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 3611726
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:54:40+00:00 2026-05-18T21:54:40+00:00

with reference of this question Sort json array I have the following JSON String

  • 0

with reference of this question Sort json array

I have the following JSON String using ajax and store the object as an array:

var homes = [
    {
     "h_id":"3",
     "city":"Dallas",
     "state":"TX",
     "zip":"75201",
     "price":"162500",
     "start_time":"2011-01-26 08:00:00",
     "end_time":"2011-01-26 05:00:00"
    },
    {
     "h_id":"4",
     "city":"Bevery Hills",
     "state":"CA",
     "zip":"90210",
     "price":"319250",
     "start_time":"2011-01-26 12:00:00",
     "end_time":"2011-01-26 05:00:00"
    },
    {
     "h_id":"5",
     "city":"New York",
     "state":"NY",
     "zip":"00010",
     "price":"962500",
     "start_time":"2011-01-28 08:00:00",
     "end_time":"2011-01-26 05:00:00"
    }
    ];

How do I create a function to sort the “start_date” field in ASC and also sort in DESC ordering using only JavaScript?

i used below function suggested by Triptych

var sort_by = function(field, reverse, primer){

   reverse = (reverse) ? -1 : 1;

   return function(a,b){

       a = a[field];
       b = b[field];

       if (typeof(primer) != 'undefined'){
           a = primer(a);
           b = primer(b);
       }

       if (a<b) return reverse * -1;
       if (a>b) return reverse * 1;
       return 0;

   }
}

and apply in below manner

// Sort by start_time
homes.sort(sort_by('start_time', false, function(a){return a.getTime()}));

but not working..:((, give this error

a.getTime is not a function

please tell me where i m doing mistake..

Thanks in advance

Note: sorry for copying same
question…

  • 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-18T21:54:41+00:00Added an answer on May 18, 2026 at 9:54 pm

    As I said in the comment, a string does not have a getTime() method. Only Date objects have. Therefore you have to convert the date string into a Date object. Change your function to:

    function(a){return (new Date(a)).getTime()}
    

    Now, there are various implementations of Date and I think only the newer ones support parsing of this kind of date string (not sure though). Maybe you have to parse the string first and pass the single values to Date:

    function(a){
        var parts = a.match(/(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/);
        var date = new Date(parts[1], parts[2], parts[3], parts[4], parts[5], parts[6]);
        return date.getTime()}
    }
    

    Reference: Date

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

Sidebar

Related Questions

This question sort of extends my other question on robots and captcha . I
So this question is a sort of follow on from here ( how to
This is sort of a follow-up to this question . If there are multiple
This question is as a result of Undefined reference question . When I try
I started out with this question , which I sort of answered there ,
I'm using JQuery to make a JSON request to access data on a private
I asked this question in the DataDynamics forum earlier today. I thought that maybe
Important: This question is getting quite long, if this is the first time you're
This is a more specific question to follow up on [another question that I
I'm reposting this question as I didn't get much of a response last time,

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.