I have a function that successfully sorts an array of JSON objects by date, but I need it to also accommodate the 13th month (salary).
module = {};
module.exports = [
{
"date": "01-2012"
},
{
"date": "12-2011"
},
{
"date": "01-2014"
},
{
"date": "08-2015"
},
{
"date": "13-2014"
}
];
document.getElementById("exports").innerHTML = JSON.stringify( module.exports ) ;
function parseMyDate( date_value ) {
return new Date( date_value.replace(/([0-9]{1,2})\-([0-9]{4})/, "$2-$1-01") );
}
module.exports.sort(function(a, b) {
return parseMyDate( a.date ) - parseMyDate( b.date );
});
document.getElementById("sorted").innerHTML = JSON.stringify( module.exports ) ;
<h1>module.exports unsorted</h1>
<pre id="exports"></pre>
<h1>module.exports sorted</h1>
<pre id="sorted"></pre>
Is there a way to make this happen?
I am working in a node.js environment, so I could easily utilize a package that enhances dates, however, I haven't been able to find one.
Thank you.