Reputation: 7117
I have an array like below
var testArr = ["2009-feb", "2009-jan", "2010-mar", "2010-jan", "2011-jul", "2011-sep", "2011-jan", "2012-jan", "2012-dec", "2012-feb", "2013-may", "2013-jul", "2013-jun", "2014-jan", "2014-dec", "2014-may", "2015-may", "2015-jan", "2015-jun", "2016-jan", "2016-dec"];
I need to sort them I did following
var sortOrder="asc";
testArr.sort(function (a, b) {
if (sortOrder == "asc") {
return a.localeCompare(b);
}
else {
return b.localeCompare(a);
}
});
The out put is
["2009-feb","2009-jan","2010-jan","2010-mar","2011-jan","2011-jul","2011-sep","2012-dec","2012-feb","2012-jan","2013-jul","2013-jun","2013-may","2014-dec","2014-jan","2014-may","2015-jan","2015-jun","2015-may","2016-dec","2016-jan"]
which is wrong.
Upvotes: 1
Views: 2031
Reputation: 4383
Use Underscore JS to sort by date
Example like :-
var newConsultantData = _.sortBy(consultantData, function(o) { return o.created; });
Upvotes: 0
Reputation: 3689
You can write your own custom comparison
method and pass that to sort
method. Here is a simple demo to do that so.
var months = [ "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"]
var testArr = ["2009-feb", "2009-jan", "2010-mar", "2010-jan", "2011-jul", "2011-sep", "2011-jan", "2012-jan", "2012-dec", "2012-feb", "2013-may", "2013-jul", "2013-jun", "2014-jan", "2014-dec", "2014-may", "2015-may", "2015-jan", "2015-jun", "2016-jan", "2016-dec"];
var testArr1 = ["2009-February", "2009-January", "2010-March", "2010-January", "2011-July", "2011-September", "2011-January", "2012-January", "2012-December", "2012-February", "2013-May", "2013-July", "2013-June", "2014-January", "2014-December", "2014-May", "2015-May", "2015-January", "2015-June", "2016-January", "2016-December"];
var sortOrder="asc";
var compare = function(a, b) {
var aParts = a.toLowerCase().split("-");
var bParts = b.toLowerCase().split("-");
var dateA = new Date(parseInt(aParts[0]), months.indexOf(aParts[1].substring(0, 3)));
var dateB = new Date(parseInt(bParts[0]), months.indexOf(bParts[1].substring(0, 3)));
if (sortOrder === "asc")
return dateA - dateB;
else
return dateB - dateA;
}
var sortedTestArr = testArr.sort(compare);
var sortedTestArr1 = testArr1.sort(compare);
document.write('<pre>' + JSON.stringify(sortedTestArr, 0, 4) + '</pre>');
document.write('<pre>' + JSON.stringify(sortedTestArr1, 0, 4) + '</pre>');
Upvotes: 1
Reputation: 28403
Try like this
var sortOrder="asc";
testArr.sort(function (a, b) {
if (sortOrder == "asc") {
return new Date(Date.Parse(b)) - new Date(Date.Parse(a));
}
else {
return new Date(Date.Parse(a)) - new Date(Date.Parse(b));
}
});
Upvotes: 0
Reputation: 386654
You could use a function which separates the date string and returns an array with the year and a month number. The month is taken from an object with month names as hashes.
function yearMonth(a, b) {
function getDate(f) {
var d = f.split('-');
d[1] = {
jan: 1, feb: 2, mar: 3, apr: 4, may: 5, jun: 6, jul: 7, aug: 8, sep: 9, oct: 10, nov: 11, dec: 12
}[d[1].substring(0, 3).toLowerCase()] || 0;
return d;
}
var aa = getDate(a),
bb = getDate(b);
return aa[0] - bb[0] || aa[1] - bb[1];
}
var testArr = ["2009-feb", "2009-jan", "2010-mar", "2010-jan", "2011-jul", "2011-sep", "2011-jan", "2012-jan", "2012-dec", "2012-feb", "2013-may", "2013-jul", "2013-jun", "2014-jan", "2014-dec", "2014-may", "2015-may", "2015-jan", "2015-jun", "2016-jan", "2016-dec"],
testArr1 = ["2009-February", "2009-January", "2010-March", "2010-January", "2011-July", "2011-September", "2011-January", "2012-January", "2012-December", "2012-February", "2013-May", "2013-July", "2013-June", "2014-January", "2014-December", "2014-May", "2015-May", "2015-January", "2015-June", "2016-January", "2016-December"];
testArr.sort(yearMonth);
testArr1.sort(yearMonth);
document.write('<pre>' + JSON.stringify(testArr, 0, 4) + '</pre>');
document.write('<pre>' + JSON.stringify(testArr1, 0, 4) + '</pre>');
Upvotes: 0
Reputation: 5926
I guess you mean sort by date then
testArr.sort(function(dateA, dateB) {
return new Date(dateA) - new Date(dateB)
})
If you want it freshest date first flip dateA with dateB.
Run example on tonic: https://tonicdev.com/lipp/sort-dates
Upvotes: 1
Reputation: 68393
For the first array, try this
var monthArray = ["jan","feb", "mar", "apr", "may", "june", "july", "aug", "sep", "oct", "nov", "dec"];
testArr.sort(function(a,b){
var ayearmonth = a.split("-");
var byearmonth = b.split("-");
var montha = monthArray.indexOf(ayearmonth);
var monthb = monthArray.indexOf(byearmonth );
if(ayearmonth[0] != byearmonth[0] )
{
return ayearmonth[0] - byearmonth[0];
}
{
return montha-monthb;
}
});
Similar approach to be used for second array.
Upvotes: 0