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.

Community
  • 1
  • 1
rusty bit
  • 371
  • 2
  • 17
  • 2
    Have you tried anything? – Ognjen Babic Feb 11 '16 at 06:54
  • 1
    Hint. You can even compare those date strings without converting them into a Date. Have you tried something? – TaoPR Feb 11 '16 at 06:57
  • @OgnjenBabic i tried solution at http://stackoverflow.com/questions/16574309/sort-json-array-by-date-key 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. – rusty bit Feb 11 '16 at 06:58

3 Answers3

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.

Jannik
  • 332
  • 1
  • 3
  • 12
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));
RobG
  • 142,382
  • 31
  • 172
  • 209
  • 1
    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**. – rusty bit Feb 11 '16 at 07:20
  • 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. – RobG Feb 11 '16 at 22:34
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>');
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392