1

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/

1
  • 1
    I edited the question to remove references to JSON and jQuery as neither are relevant to the issue. Commented Jun 22, 2018 at 9:17

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

Comments

0

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>

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.