0

Below is my json data:

var homes={
"ERROR": "SUCCESS",
"DATA": [
    {
        "BookingID": "9513",
        "DutyStart": "2016-02-11 12:00:00"
    },
    {
        "BookingID": "91157307",
        "DutyStart": "2016-02-11 13:00:00"
    },
    {
        "BookingID": "95117317",
        "DutyStart": "2016-02-11 13:30:00"
    },
    {
        "BookingID": "957266",
        "DutyStart": "2016-02-12 19:15:00"
    },
    {
        "BookingID": "74",
        "DutyStart": "2016-02-11 12:21:00"
    }
]
};

I want to sort the array according to DutyStart(in date format) in Descending and Ascending order using Javascript only and append the data to body or alert the sorted array.

I tried the solution fromThis question but iam not able to get it.

Thanks in advance.

3
  • 2
    Have you tried anything? Commented Feb 11, 2016 at 6:54
  • 1
    Hint. You can even compare those date strings without converting them into a Date. Have you tried something? Commented Feb 11, 2016 at 6:57
  • @OgnjenBabic i tried solution at stackoverflow.com/questions/16574309/… but the issue is that the are creating a json like [{...}] but i have {a:xxx, b:[{..}]}. as iam new to this iam not able to get it. Commented Feb 11, 2016 at 6:58

3 Answers 3

1

You can sort very easily on ISO 8601-like date strings since they work as strings as well as numbers would.

Pass the values to a sort function and return 0, 1 or -1 depending the comparison, e.g.

// Data
var homes={
"ERROR": "SUCCESS",
"DATA": [
    {
        "BookingID": "9513",
        "DutyStart": "2016-02-11 12:00:00"
    },
    {
        "BookingID": "91157307",
        "DutyStart": "2016-02-11 13:00:00"
    },
    {
        "BookingID": "95117317",
        "DutyStart": "2016-02-11 13:30:00"
    },
    {
        "BookingID": "957266",
        "DutyStart": "2016-02-12 19:15:00"
    },
    {
        "BookingID": "74",
        "DutyStart": "2016-02-11 12:21:00"
    }
]
};

// Sort - operates on the original array
homes.DATA.sort(function(a, b) {
  return a.DutyStart < b.DutyStart? -1 : (a.DutyStart == b.DutyStart? 0 : 1); 
});

document.write(JSON.stringify(homes.DATA));

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @RobG. can u please explain what this means Pass the values to a sort function and return 0, 1 or -1 depending the comparison and how to sort in ascending and descending.
It's a explanation of the code. You can reverse the order of the sort by reversing the comparison outcome, so instead of a < b? -1 : (a == b? 0 : 1) swap -1 and 1: a < b? 1 : (a == b? 0 : -1). There are many questions here about sorting arrays, search for some.
1

Simple bubble sort for your data, sorting by the date:

var homes={
"ERROR": "SUCCESS",
"DATA": [
    {
        "BookingID": "9513",
        "DutyStart": "2016-02-11 12:00:00"
    },
    {
        "BookingID": "91157307",
        "DutyStart": "2016-02-11 13:00:00"
    },
    {
        "BookingID": "95117317",
        "DutyStart": "2016-02-11 13:30:00"
    },
    {
        "BookingID": "957266",
        "DutyStart": "2016-02-12 19:15:00"
    },
    {
        "BookingID": "74",
        "DutyStart": "2016-02-11 12:21:00"
    }
]
};


var list = homes.DATA.sort(function(a, b) {
    return a.DutyStart - b.DutyStart;
});
console.log(list);

Hope this helps.

2 Comments

Why not something like homes.DATA.sort(sortFn(a, b))?
Thanks @RobG. Updated answer to reflect
0

I suggest to use String#localeCompare for this task, because it compares strings and while the data are ISO 8601 date strings, they can be sorted directly with this method.

var homes = { "ERROR": "SUCCESS", "DATA": [{ "BookingID": "9513", "DutyStart": "2016-02-11 12:00:00" }, { "BookingID": "91157307", "DutyStart": "2016-02-11 13:00:00" }, { "BookingID": "95117317", "DutyStart": "2016-02-11 13:30:00" }, { "BookingID": "957266", "DutyStart": "2016-02-12 19:15:00" }, { "BookingID": "74", "DutyStart": "2016-02-11 12:21:00" }] };

homes.DATA.sort(function (a, b) {
    return a.DutyStart.localeCompare(b.DutyStart);
});

document.write('<pre>' + JSON.stringify(homes, 0, 4) + '</pre>');

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.