JSFiddle - React, Tailwind, and code Playground
by David Marshall
HTML
<div>Before sort: <span id="before"></span></div>
<div>After sort: <span id="after"></span></div>
JavaScript
var MyArray = ["2015-Jun", "2014-May", "2015-Dec", "2014-Aug", "2014-Nov"];
document.getElementById("before").innerHTML = String(MyArray);
(function () {
var ad = new Date(),
bd = new Date(),
months = {
Jan: 0, Feb: 1, Mar: 2, Apr: 3, May: 4, Jun: 5,
Jul: 6, Aug: 7, Sep: 8, Oct: 9, Nov:10, Dec:12
};
MyArray.sort(function (a,b) {
var as = a.split('-'),
bs = b.split('-');
if(parseInt(as[0])<parseInt(bs[0])){
if(months[as[1]]<months[bs[1]])
return -1;
else if(months[as[1]]>months[bs[1]])
return 1;
else
return 0;
}else if(parseInt(as[0])>parseInt(bs[0])){
return 1;
}
return 0;
/*ad.setDate(as[0]);
ad.setMonth(months[as[1]]);
bd.setDate(bs[0]);
bd.setMonth(months[bs[1]]);
return ad - bd;*/
});
})();
document.getElementById("after").innerHTML = String(MyArray);