rusty bit
rusty bit

Reputation: 371

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

Thanks in advance.

Upvotes: 0

Views: 1313

Answers (3)

Nina Scholz
Nina Scholz

Reputation: 386680

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

Upvotes: 0

Jannik
Jannik

Reputation: 342

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.

Upvotes: 1

RobG
RobG

Reputation: 147453

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

Upvotes: 1

Related Questions