Pawan
Pawan

Reputation: 32321

How to sort an array of objects based on date

I have an array of objects which I am attempting to sort based on the createDate.

[{
    "TestName": "com.DPProgram",
    "Test": {
        "createDate": "2018-02-15T17:17:10.000+0530",
        "effectiveStartDate": "1900-01-01T00:00:00.000+0530",
        "effectiveEndDate": "2200-01-01T00:00:00.000+0530"
    }
}, {
    "TestName": "com.DPProgram",
    "Test": {
        "createDate": "2018-02-22T15:00:11.000+0530",
        "effectiveStartDate": "2017-12-22T00:00:00.000+0530",
        "effectiveEndDate": "2018-12-23T00:00:00.000+0530"
    }
}];

data = data.sort(function(a, b) {
  data = data.sort(function(a, b) {
    return (a[data.createDate] > b[data.createDate]) 
  });

However it's not sorting

https://jsfiddle.net/o2gxgz9r/51545/

Upvotes: 1

Views: 74

Answers (3)

GuyT
GuyT

Reputation: 21

When you use a sort function, you get two arguments that are elements of the data array you are operating on, and so you need to use them:

data.sort(function(a, b) {
    return a.Test.createDate - b.Test.createDate;
});

Upvotes: 0

Parth Raval
Parth Raval

Reputation: 4413

var data = [{
    "TestName": "com.DPProgram",
    "Test": {
        "createDate": "2018-02-15T17:17:10.000+0530",
        "effectiveStartDate": "1900-01-01T00:00:00.000+0530",
        "effectiveEndDate": "2200-01-01T00:00:00.000+0530"
    }
}, {
    "TestName": "com.DPProgram",
    "Test": {
        "createDate": "2018-02-22T15:00:11.000+0530",
        "effectiveStartDate": "2017-12-22T00:00:00.000+0530",
        "effectiveEndDate": "2018-12-23T00:00:00.000+0530"
    }
},{
    "TestName": "com.DPProgram",
    "Test": {
        "createDate": "2018-02-25T15:00:11.000+0530",
        "effectiveStartDate": "2017-12-22T00:00:00.000+0530",
        "effectiveEndDate": "2018-12-23T00:00:00.000+0530"
    }
}];
console.log(_.sortBy(data, 'createDate'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore.js"></script>

Upvotes: 0

Rory McCrossan
Rory McCrossan

Reputation: 337560

You are comparing the dates as strings. You need to instead convert them to Date objects before making the comparison.

Also your syntax of a[data.bean.createDate] is broken given the example. You need to access obj.Test.createDate instead.

Finally, don't use alert() for debugging, and especially not for any datatype more complex than a string. console.log() is more accurate as it doesn't coerce data types, and lets you traverse through the levels or objects/arrays.

With all that said, try this:

var data = [{
  "TestName": "com.DPProgram",
  "Test": {
    "createDate": "2018-02-15T17:17:10.000+0530",
    "effectiveStartDate": "1900-01-01T00:00:00.000+0530",
    "effectiveEndDate": "2200-01-01T00:00:00.000+0530"
  }
}, {
  "TestName": "com.callidus.quotaDP.Tests.DPProgram",
  "Test": {
    "createDate": "2018-02-22T15:00:11.000+0530",
    "effectiveStartDate": "2017-12-22T00:00:00.000+0530",
    "effectiveEndDate": "2018-12-23T00:00:00.000+0530"
  }
}, {
  "TestName": "com.Foo",
  "Test": {
    "createDate": "2018-02-07T15:00:11.000+0530",
    "effectiveStartDate": "2017-12-22T00:00:00.000+0530",
    "effectiveEndDate": "2018-12-23T00:00:00.000+0530"
  }
}];

data = data.sort(function(a, b) {
  var aDate = new Date(a.Test.createDate),
    bDate = new Date(b.Test.createDate);
  return aDate > bDate ? 1 : aDate < bDate ? -1 : 0;

});

console.log(data);

Upvotes: 3

Related Questions