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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:22:35+00:00 2026-05-27T07:22:35+00:00

I get back a json object from Facebook which contains some friends information. Some

  • 0

I get back a json object from Facebook which contains some friends information.

Some users have included their birthday some have not, while others have included only the the month and day.

I want to sort the array putting users with a birthday that is closes to the current date first.

How can I do this?

The json object looks like this:

json = { "data" : [{name : "Joe Sam", id : "5555555", birthday: "02/02/1989" }, {name : "Joe Sam", id : 5555555, birthday:  }, {name : "Joe Sam", id : 5555555, birthday: "01/01" }
  • 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-27T07:22:36+00:00Added an answer on May 27, 2026 at 7:22 am

    Your JSON is invalid – if that is the actual JSON string keynames need to be quoted. You have left off the closing ] and }, and the middle record’s birthday has to have some kind of value, e.g., empty string or null – or just don’t provide that key at all. I’ll assume you can fix that and will have already parsed the JSON into a variable called json.

    Also you don’t say if the dates are in DD/MM(/YYYY) format or MM/DD(/YYYY) format so I’ll code for DD/MM but you can comment that out to use MM/DD instead.

    “Closest to the current date” is ambiguous: is yesterday closer than next week? I shall assume that yesterday is as far from the current date as you can get.

    So here’s your object tidied up along with a sort routine. I haven’t tested it, but even assuming it is broken it should give you the general idea:

    var json = { "data" : [
                    {name : "Joe Sam", id : "5555555", birthday: "02/02/1989" },
                    {name : "Joe Sam", id : 5555555, birthday: null },
                    {name : "Joe Sam", id : 5555555, birthday: "01/01" }
                 ]
               };
    
    // First sort into ascending birthday order, with people who didn't provide
    // a birthday at the beginning of the list
    
    function dayMonthComparer(a,b)
      // note double-equals null also allows for undefined "birthday" property 
      if (aBD == null)
        return bBD == null ? 0 : -1;
    
      if (bBD == null)
        return 1;
    
      // next two lines allow for DD/MM format; comment them out for MM/DD format
      aBD = aBD.substr(3,2) + aBD.substr(0,2);
      bBD = bBD.substr(3,2) + bBD.substr(0,2);
    
      // note: simple string compare works once in MM/DD format
      return aBD === bBD ? 0 : (aBD > bBD ? 1 : -1);
    }
    
    json["data"].sort(function(a,b) {
      return dayMonthComparer(a["birthday"],b["birthday"]);
    });
    
    // Next, find the first item in the array after the current date and
    // move everything before that item to the end of the array.
    var today = new Date(),
        d = today.getDate(),
        m = today.getMonth() + 1,
        current,
        firstNonBlank = null,
        firstFromCurrent = 0;
    
    if (d < 10) d = "0" + d;
    if (m < 10) d = "0" + d;
    
    current = d + "/" m;
    // or use current = m + "/" + d if using American format
    
    // get index of first item with birthday on or after current date
    while(firstFromCurrent < json["data"].length &&
          dayMonthComparer(current,json["data"][firstFromCurrent]["birthday"]) > 1) {
      if (firstNonBlank===null &&
          json["data"][firstFromCurrent]["birthday"] != null)
         firstNonBlank = firstFromCurrent;
      firstFromCurrent++;
    }
    
    if (firstFromCurrent < json["data"].length) {
      json["data"] = json["data"].slice(firstFromCurrent)
                     .concat(json["data"].slice(firstNonBlank,firstFromCurrent),
                             json["data"].slice(0,firstNonBlank) );
    }
    
    // array is now sorted by birthday starting from current date, where
    // those who didn't provide a birthday are at the end
    

    For details about how .sort() works refer to the MDN doco.

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

Sidebar

Related Questions

I have a Servlet that sends back a JSON Object and I would like
I have a Web Service and it always brings back one object from my
I have some simple .NET objects I'd like to serialize to JSON and back
I am making an AJAX calling using JQuery. I get back a JSON object
I'm trying to get a json serialized object from within an MVC user control
I am passing back a JSON object (consisting of an array of strings) from
I have a javascript function which asks for some ajax data and gets back
I am using MVC. I have a view model which contains a Business object.
I access a RESTFUL url and get back results. The results are in JSON.
How to get back from encoded byte[] to java.security.Key? import java.security.Key; import javax.crypto.SecretKey; import

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.