简体   繁体   中英

Javascript - Sort nested array containing objects ascending and descending according to date

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 from This question but iam not able to get it.

Thanks in advance.

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.

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, eg

 // 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)); 

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>'); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM