2

So i'm trying to sort a JSON array by the date key, currently the problem seems to be the function stops after one sort (or just is plain wrong).

The following is my sort js

  function sortByDate() {
        result = gloresult
        var newA = result.sort(function(a,b){
          return Number(new Date(a.Date)) - Number(new Date(b.Date));
        });

       console.log(newA)


      }

Input Json file

gloresult = [
    {
        "Heading": "A",
        "Topic A": "Ball Valve",
        "Date": "2/05/2019"
    },
    {
        "Heading": "B",
        "Topic A": "ABS",
        "Date": "1/05/2019"
    },
    {
        "Heading": "C",
        "Topic A": "Acrylic",
        "Date": "21/05/2019"
    },
    {
        "Heading": "D",
        "Topic A": "Adaptor Fitting",
        "Date": "21/05/2019"
    },
    {
        "Heading": "E",
        "Topic A": "Air Gap",
        "Date": "4/05/2019"
    },
    {
        "Heading": "F",
        "Topic A": "Stuff",
        "Date": "21/03/2019"
    },
    {
        "Heading": "G",
        "Topic A": "Stuff",
        "Date": "21/04/2019"
    },
    {
        "Heading": "H",
        "Topic A": "Stuff",
        "Date": "21/05/2021"
    }

]

Output Json file

[
    {
        "Heading": "B",
        "Topic A": "ABS",
        "Date": "1/05/2019"

    },
    {
        "Heading": "A",
        "Topic A": "Ball Valve",
        "Date": "2/05/2019"

    },
    {
        "Heading": "C",
        "Topic A": "Acrylic",
        "Date": "21/05/2010"

    },
    {
        "Heading": "D",
        "Topic A": "Adaptor Fitting",
        "Date": "21/05/2019"

    },
    {
        "Heading": "E",
        "Topic A": "Air Gap",
        "Date": "4/05/2019"

    },
    {
        "Heading": "F",
        "Topic A": "Stuff",
        "Date": "21/03/2019"

    },
    {
        "Heading": "G",
        "Topic A": "Stuff",
        "Date": "21/04/2019"

    },
    {
        "Heading": "H",
        "Topic A": "Stuff",
        "Date": "21/05/2021"
    }
]

As you can see only A and B have changed places and the result have remained completely the same. I'm not sure if this is because i'm calling the function when a user selects a button on a html page.

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
chris
  • 107
  • 10

2 Answers2

3

Because the dates are in DD/MM/YYYY format. The Date constructor expects MM/DD/YYYY format. (a new date in DD/MM/YYYY format, if invalid, results in NaN when passed to Number). Just change your code inside the function slightly to fix this:

const gloresult = [{"Heading":"A","Topic A":"Ball Valve","Date":"2/05/2019"},{"Heading":"B","Topic A":"ABS","Date":"1/05/2019"},{"Heading":"C","Topic A":"Acrylic","Date":"21/05/2019"},{"Heading":"D","Topic A":"Adaptor Fitting","Date":"21/05/2019"},{"Heading":"E","Topic A":"Air Gap","Date":"4/05/2019"},{"Heading":"F","Topic A":"Stuff","Date":"21/03/2019"},{"Heading":"G","Topic A":"Stuff","Date":"21/04/2019"},{"Heading":"H","Topic A":"Stuff","Date":"21/05/2021"}];

const sortByDate = () => {
  let result = gloresult;
  let newA = result.sort(({ Date: a }, { Date: b }) => {
    let [da, ma, ya] = a.split("/");
    let [db, mb, yb] = b.split("/");
    return Number(new Date([ma, da, ya].join("/"))) - Number(new Date([mb, db, yb].join("/")));
  });
  console.log(newA);
}

sortByDate();
.as-console-wrapper { max-height: 100% !important; top: auto; }
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
  • Ah i see, perfect, thanks – chris May 26 '19 at 00:07
  • No problem @chris, always glad to help. – Jack Bashford May 26 '19 at 00:10
  • "*The Date constructor expects…*" no it doesn't. The parsing of any format other than the 3 defined by ECMA-262 is implementation dependent. Support for m/d/y is a legacy feature and not standardised anywhere. Instead of creating Dates, you could just reformat the values as ISO 8601 date strings and let them sort lexically. Or combine the values them as numbers (yyyymmdd) and sort them numerically. ;-) – RobG May 26 '19 at 05:36
0

It was a little tricky, but it's works !

var
  gloresult = [ { "Heading": "A", "Topic A": "Ball Valve",      "Date": "2/05/2019"   }
              , { "Heading": "B", "Topic A": "ABS",             "Date": "1/05/2019"   }
              , { "Heading": "C", "Topic A": "Acrylic",         "Date": "21/05/2019"  }
              , { "Heading": "D", "Topic A": "Adaptor Fitting", "Date": "21/05/2019"  }
              , { "Heading": "E", "Topic A": "Air Gap",         "Date": "4/05/2019"   }
              , { "Heading": "F", "Topic A": "Stuff",           "Date": "21/03/2019"  }
              , { "Heading": "G", "Topic A": "Stuff",           "Date": "21/04/2019"  }
              , { "Heading": "H", "Topic A": "Stuff",           "Date": "21/05/2021"  }
              ];

gloresult.sort((a, b)=>{
  let
    aa = a.Date.split('/').reverse().map(d=>('0000'+d).slice(-4)).join('-'),
    bb = b.Date.split('/').reverse().map(d=>('0000'+d).slice(-4)).join('-');
  return aa < bb ? -1 : (aa > bb ? 1 : 0);
});

// for (let x of gloresult ) console.log ( x.Date );
for (let x of gloresult ) console.log ( JSON.stringify( x ) );
.as-console-wrapper { max-height: 100% !important; top: auto; }
Mister Jojo
  • 20,093
  • 6
  • 21
  • 40