Currently, I am attempting to organize a JSON array based on the date key. However, it appears that my sorting function is either stopping after one sort or simply not functioning correctly.
Here is the JavaScript code for my sorting function:
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 content
gloresult = [
{
"Heading": "A",
"Topic A": "Ball Valve",
"Date": "2/05/2019"
},
{
"Heading": "B",
"Topic A": "ABS",
"Date": "1/05/2019"
},
//more data...
]
Output Json content
[
{
"Heading": "B",
"Topic A": "ABS",
"Date": "1/05/2019"
},
{
"Heading": "A",
"Topic A": "Ball Valve",
"Date": "2/05/2019"
},
//more sorted data...
]
Upon examination, I noticed that only items A and B have switched positions while the rest of the results remain unchanged. I suspect this could be due to the way I am calling the function when a user selects a button on an HTML page.