Ahmad Abbasi
Ahmad Abbasi

Reputation: 1776

Sort JSON data by date javascript

I am trying to sort my json data by date but its not working. here is what I am trying. please correct me where I am making mistake

Sample Code

var temp = [{
    "id": 17608,
    "title": "abc",
    "start": "2016-03-23 06:13:00.0",
    "backgroundColor": "#000000",
    "borderColor": "#000000",
    "textColor": "#fff"
}, {
    "id": 17608,
    "title": "def",
    "start": "2016-04-13 06:13:00.0",
    "backgroundColor": "#000000",
    "borderColor": "#000000",
    "textColor": "#fff"
}, {
    "id": 17608,
    "title": "ghi",
    "start": "2016-04-08 06:13:00.0",
    "backgroundColor": "#000000",
    "borderColor": "#000000",
    "textColor": "#fff"
}];

console.log(temp);

temp.sort(function(a, b) {
    if (new Date(a.start) == new Date(b.start)) {
        return a.row == b.row ? 0 : +a.row > +b.row ? 1 : -1;
    }

    return new Date(a.start) > (b.start) ? 1 : -1;
});

console.log(temp);

Upvotes: 1

Views: 118

Answers (7)

Asadbek Eshboev
Asadbek Eshboev

Reputation: 111

        var temp = [{
        "id": 17608,
        "title": "abc",
        "start": "2016-03-23 06:13:00.0",
        "backgroundColor": "#000000",
        "borderColor": "#000000",
        "textColor": "#fff"
    }, {
        "id": 17608,
        "title": "def",
        "start": "2016-04-13 06:13:00.0",
        "backgroundColor": "#000000",
        "borderColor": "#000000",
        "textColor": "#fff"
    }, {
        "id": 17608,
        "title": "ghi",
        "start": "2016-04-08 06:13:00.0",
        "backgroundColor": "#000000",
        "borderColor": "#000000",
        "textColor": "#fff"
    }];
    
    
    
    const groups = this.temp.reduce((groups, data) => {
        const date = data.start.split(' ')[0];
        if (!groups[date]) {
          groups[date] = [];
        }
        groups[date].push(data);
        return groups;
      }, {});
    
      // Edit: to add it in the array format instead
      const groupArrays = Object.keys(groups).map((date) => {
        return {
          date,
          temps: groups[date]
        };
      });

console.log(groupArrays)

Upvotes: 0

Asadbek Eshboev
Asadbek Eshboev

Reputation: 1

  const groups = this.posts.reduce((groups, data) => {
    const date = data.published_date.split(' ')[0];
    if (!groups[date]) {
      groups[date] = [];
    }
    groups[date].push(data);
    return groups;
  }, {});

  // Edit: to add it in the array format instead
  const groupArrays = Object.keys(groups).map((date) => {
    return {
      date,
      posts: groups[date]
    };
  });

do this, it should work!

Upvotes: 0

heyo
heyo

Reputation: 19

var temp = [{
    "id": 17608,
    "title": "abc",
    "start": "2016-03-23 06:13:00.0",
    "backgroundColor": "#000000",
    "borderColor": "#000000",
    "textColor": "#fff"
}, {
    "id": 17608,
    "title": "def",
    "start": "2016-04-13 06:13:00.0",
    "backgroundColor": "#000000",
    "borderColor": "#000000",
    "textColor": "#fff"
}, {
    "id": 17608,
    "title": "ghi",
    "start": "2016-04-08 06:13:00.0",
    "backgroundColor": "#000000",
    "borderColor": "#000000",
    "textColor": "#fff"
}];

console.log(temp);

temp.sort(function(a, b) {
    return parseFloat(a.start) - parseFloat(b.start);
});

console.log(temp);

Upvotes: -1

Emil Oberg
Emil Oberg

Reputation: 4046

There are multiple ways of achieving this. Of them, the easiest would be to convert the strings to dates and substract them from each other to get a negative, positive or zero number:

temp.sort(function(a,b){
  return new Date(a.start) - new Date(b.start);
});

It is often believed that the sort function needs to return -1, 1 or 0. This is simply not true. It will sort the items based on if the number is positive, negative or zero. The ECMAScript specification states it as:

If comparefn is not undefined, it should be a function that accepts two arguments x and y and returns a negative value if x < y, zero if x = y, or a positive value if x > y.

Full example:

var temp = [{
    "id": 17608,
    "title": "abc",
    "start": "2016-03-23 06:13:00.0",
    "backgroundColor": "#000000",
    "borderColor": "#000000",
    "textColor": "#fff"
}, {
    "id": 17608,
    "title": "def",
    "start": "2016-04-13 06:13:00.0",
    "backgroundColor": "#000000",
    "borderColor": "#000000",
    "textColor": "#fff"
}, {
    "id": 17608,
    "title": "ghi",
    "start": "2016-04-08 06:13:00.0",
    "backgroundColor": "#000000",
    "borderColor": "#000000",
    "textColor": "#fff"
}];

console.log(temp);

temp.sort(function(a,b){
  // Convert strings to dates and substract.
  // This way you get a value which is negative, positive or zero
  return new Date(a.start) - new Date(b.start);
});

console.log(temp);

Upvotes: 2

haim770
haim770

Reputation: 49095

Your code is fine, it's just that you're missing the second new Date() in your comparison:

return new Date(a.start) > (b.start) ? 1 : -1;

Should be:

return new Date(a.start) > new Date(b.start) ? 1 : -1;

The way it is now, you're simply comparing a Date object to string.

Upvotes: 2

Rajesh
Rajesh

Reputation: 24925

You should use date.getTime() while comparing dates.

var temp= [{id:17608,title:"abc",start:"2016-03-23 06:13:00.0",backgroundColor:"#000000",borderColor:"#000000",textColor:"#fff"},{id:17608,title:"def",start:"2016-04-13 06:13:00.0",backgroundColor:"#000000",borderColor:"#000000",textColor:"#fff"},{id:17608,title:"ghi",start:"2016-04-08 06:13:00.0",backgroundColor:"#000000",borderColor:"#000000",textColor:"#fff"}];

console.log(temp);

temp.sort(function(a, b) {
  var d1 = new Date(a.start).getTime();
  var d2 = new Date(b.start).getTime();
  return d1<d2?-1:d1>d2?1:0;
});

console.log(temp);

Upvotes: 2

Nina Scholz
Nina Scholz

Reputation: 386654

You can use the date string for sorting, while it is an ISO 6801 date.

var temp = [{ "id": 17608, "title": "abc", "start": "2016-03-23 06:13:00.0", "backgroundColor": "#000000", "borderColor": "#000000", "textColor": "#fff" }, { "id": 17608, "title": "def", "start": "2016-04-13 06:13:00.0", "backgroundColor": "#000000", "borderColor": "#000000", "textColor": "#fff" }, { "id": 17608, "title": "ghi", "start": "2016-04-08 06:13:00.0", "backgroundColor": "#000000", "borderColor": "#000000", "textColor": "#fff" }];

temp.sort(function (a, b) {
    return a.start.localeCompare(b.start);
});

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

Upvotes: 6

Related Questions