admin管理员组

文章数量:1401214

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?

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?

Share Improve this question asked May 15, 2013 at 13:07 Daniela costina VaduvaDaniela costina Vaduva 1,8975 gold badges20 silver badges22 bronze badges 8
  • 3 The best function that suits your needs is Array.sort(). – VisioN Commented May 15, 2013 at 13:11
  • @VisioN it also works for my array of arrays? – Daniela costina Vaduva Commented May 15, 2013 at 13:13
  • 1 @DanielacostinaVaduva if you send your own parison function as a strategy object it will – Mortalus Commented May 15, 2013 at 13:14
  • @DanielacostinaVaduva It works for any JavaScript arrays. And it doesn't care about the contents of these arrays, i.e. what data is stored there. – VisioN Commented May 15, 2013 at 13:14
  • @Mortalus Strategy object? This could be something like .getDay() method? – Daniela costina Vaduva Commented May 15, 2013 at 13:16
 |  Show 3 more ments

3 Answers 3

Reset to default 4

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 patibility with Safari
       See also http://stackoverflow./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.

You can use the sort function as VisioN suggested and pass your own parison 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 info about sort: http://www.w3schools./jsref/jsref_sort.asp

whit regards to my "Strategy Object" ment, here is some more information: http://en.wikipedia/wiki/Strategy_pattern

If the goal is to create a separate array for each day of the week, a sort is not necessary

 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"]]
]"

本文标签: jqueryJavaScript sort an array of arrays by the day of the weekStack Overflow