Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript sort an array of arrays by the day of the week

I have the following variable (console.log(response)):

"['2013-04-15', 26]", "['2013-04-16', 10]", "['2013-04-17', 51]", "['2013-04-18', 46]", "['2013-04-19', 32]", "['2013-04-20', 50]", "['2013-04-21', 26]", "['2013-04-22', 31]", "['2013-04-23', 48]", "['2013-04-24', 821]", "['2013-04-25', 917]", "['2013-04-26', 949]", "['2013-04-27', 405]", "['2013-04-28', 593]", "['2013-04-29', 925]", "['2013-04-30', 877]", "['2013-05-01', 277]", "['2013-05-02', 112]", "['2013-05-03', 115]", "['2013-05-04', 62]", "['2013-05-05', 74]", "['2013-05-06', 76]", "['2013-05-07', 51]", "['2013-05-08', 93]", "['2013-05-09', 231]", "['2013-05-10', 350]", "['2013-05-11', 258]", "['2013-05-12', 0]", "['2013-05-13', 61]"

which I transform in an array of arrays in the following manner:

var json = response.replace(/"/g,'');
json = "[" + json + "]";
json = json.replace(/'/g,'"');
var myData = JSON.parse(json);

and I receive (console.log(myData)):

myData = [Array[2], Array[2], Array[2], Array[2], Array[2], Array[2], Array[2], Array[2], Array[2], Array[2], Array[2], Array[2], Array[2], Array[2], Array[2], Array[2], Array[2], Array[2], Array[2], Array[2], Array[2], Array[2], Array[2], Array[2], Array[2], Array[2], Array[2], Array[2], Array[2]]

which I need to keep in this format for further usage. I want to know if is possible to sort the response by the day of the week? And also if is possible to store in a variable for example only the monday days, tuesday days in another one and so on? I have to use JQuery for this, is even a function that suits my needs?

like image 270
Daniela costina Vaduva Avatar asked Mar 23 '23 22:03

Daniela costina Vaduva


1 Answers

You can use method sort() passing your custom sorter function as parameter (see below).
In order to get the day-of-week of the date corresponding to each array's 1st element (e.g. "2013-04-15"), you can use Date's getDay() function.

var sorter = function(a, b) {
    /* The '.replace("-", "/")' part is for compatibility with Safari
       See also http://stackoverflow.com/questions/4310953/invalid-date-in-safari */
    var d1 = new Date(a[0].replace("-", "/")).getDay();
    var d2 = new Date(b[0].replace("-", "/")).getDay();
    return d1 - d2;
};
myData.sort(sorter);

NOTE:
getDay() returns an integer between 0 and 6 (inclusive), which correspond to days-of-week Sunday through Saturday (Sunday is 0, Monday is 1...).

If you want to classify them by day-of-week instead of sorting, you can use something like this:

function classifyByDayOfWeek(customArr) {
    var byDayOfWeek = [[], [], [], [], [], [], []];
    for (var i = 0; i < customArr.length; i++) {
        var day = new Date(customArr[i][0]).getDay();
        byDayOfWeek[day].push(customArr[i]);
    };
    return byDayOfWeek;
}
myData = classifyByDayOfWeek(myData);

See also this short demo.

like image 139
gkalpak Avatar answered Apr 10 '23 08:04

gkalpak