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.