-1

I am trying to sort this JavaScript object array based on the Planned Start

[{"EPRID":"123","AssetName":"AS1","Identifier":"","ID":"C399","Category":"blank","This_ID":"0023-E","Approval status":"Need More Information","Status":"initial","Phase":"Implementation","Planned Start Date":"10/31/2017","Planned End Date":"22/11/2017","Description":"as1 testing","Requestor":"bob","Comments":"test comment","Comment_Updated_By":"jim","Comment_Updated_Timestamp":"09/14/2017 08:00:55"},
{"EPRID":"321","AssetName":"AS3","Identifier":"C19","ID":null,"Category":"Normal Changes","This_ID":"0013-E","Approval status":null,"Status":"initial","Phase":"Implementation","Planned Start Date":"11/21/2016","Planned End Date":"12/12/2016","Description":"as3 testing","Requestor":"joe","Comments":null,"Comment_Updated_By":null,"Comment_Updated_Timestamp":null},
{"EPRID":"213","AssetName":"AS5","Identifier":"C113","ID":null,"Category":"Normal Changes","This_ID":"0143-E","Approval status":null,"Status":"initial","Phase":"Authorization","Planned Start Date":"11/05/2017","Planned End Date":"11/05/2017","Description":"as5 testing","Requestor":"john","Comments":null,"Comment_Updated_By":null,"Comment_Updated_Timestamp":null}]  

I have tried the following:

rowObj.sort(function(a, b) {
        return a["Planned Start Date"] < b["Planned Start Date"];
});

Which I found from 979256

I have also tried the localeCompare() but still can seem to get my desired result.

JMP
  • 2,299
  • 17
  • 26
  • 34
user3292394
  • 479
  • 1
  • 7
  • 19
  • 3
    The function given to `sort` should return -1, 0 or 1. Not true or false. See [the docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) – Nick Sep 14 '17 at 13:20
  • 1
    well your "date" is a "string" so you are not comparing dates, you are comparing strings. So you need to convert your string into a date object. – epascarello Sep 14 '17 at 13:20
  • https://stackoverflow.com/questions/7151543/convert-dd-mm-yyyy-string-to-date – epascarello Sep 14 '17 at 13:24

3 Answers3

4
Try this: 

rowObj.sort(function (a, b) {

   let aStartDate=new Date(a["Planned Start Date"]);
   let bStartDate=new Date(b["Planned Start Date"]);

   if (aStartDate.getTime() > bStartDate.getTime()) {
       return 1;
     }

   if (aStartDate.getTime() < bStartDate.getTime()) {
       return -1;
     }

    return 0;
});

console.log(rowObj);
Ehab Qadah
  • 460
  • 4
  • 9
0

The sort in javascript is a string sort. But you want to compare dates. So convert the string to date and then compare

rowObj.sort(function(a, b) {
    return new Date(a["Planned Start Date"]) < new Date(b["Planned Start Date"]);
});
soumya sunny
  • 206
  • 1
  • 7
0

You need to convert the compare values to date and return a value of either 0, 1, or -1.

arr.sort(function(a,b){
   return new Date(a['Planned Start Date']) - new Date(b['Planned Start Date']);
});

or reversed:

arr.sort(function(a,b){
   return new Date(b['Planned Start Date']) - new Date(a['Planned Start Date']);
});

See this post for more details: Sort Javascript Object Array By Date

lance-p
  • 936
  • 10
  • 23