JavaScript sorts an array of arrays by 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 convert to an array of arrays as follows:

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

and I get ( 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 save in this format for future use. I want to know if it is possible to sort the answer by the day of the week? And also, if you can save in a variable, for example, only on Monday, days of the day in another, and so on? Should I use jQuery for this, even a function that suits my needs?

+4
source share
3 answers

You can use the sort() method to pass your custom sorter function as a parameter (see below).
To get the day of the week corresponding to each element of the 1st element (for example, "2013-04-15"), you can use Date getDay() .

 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 from 0 to 6 (inclusive), which corresponds to the days of the week on Sunday through Saturday (Sunday - 0, Monday - 1 ...).

If you want to classify them by day of the 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 a short demo .

+4
source

You can use the sort function at the suggestion of VisioN and pass your own comparison strategy. Something like this should work for you:

 myData.sort(function(a,b) { if ((a[0]>b[0]) && (a[1]>b[1])) return 1 if ((a[0]==b[0]) && (a[1]==b[1])) return 0 if ((a[0]<b[0]) && (a[1]<b[1])) return -1 }); 

more sorting information: http://www.w3schools.com/jsref/jsref_sort.asp

as for my comment โ€œObject of Strategyโ€, here is some more information: http://en.wikipedia.org/wiki/Strategy_pattern

+2
source

If the goal is to create a separate array for each day of the week, sorting is not required.

  myData = ["['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]"]; myData = myData.map (function (v) { // convert to useable form v = v.match (/^\['(\d{4}-\d{2}-\d{2})',\s*(\d+)\]$/); return [v[1], v[2]]; }); myDays = myData.reduce (function (days, v) { days[(new Date (v[0])).getDay ()].push (v); return days; }, [[],[],[],[],[],[],[]]); // array of 7 arrays, one per day of week. JSON.stringify (myDays); 

->

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

Source: https://habr.com/ru/post/1480950/


All Articles