Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort array by month-year and week- year Javascript

I have an array as follows :

var monthYear = ["1-2016", "7-2015", "7-2016", "3-2016", "8-2016", "2-2016", "6-2016", "8-2015", "5-2016", "4-2016", "9-2015", "10-2015", "11-2015", "12-2015"];

In this array shows me the month and year , I want to sort by month and year as follows :

var monthYear = ["7-2015", "8-2015", "9-2015", "10-2015", "11-2015", "12-2015", "1-2016", "2-2016", "3-2016", "4-2016", "5-2016", "6-2016", "7-2016", "8-2016"];

Similarly with the weeks

var weekYear = ["2015-30", "2015-31", "2015-32", "2015-33", "2015-34", "2015-35", "2015-36", "2015-37", "2015-38", "2015-39", "2015-40", "2015-41", "2015-42", "2015-44", "2015-49", "2016-06", "2016-09", "2015-43", "2015-45", "2015-46", "2015-47", "2015-48", "2015-50", "2015-51", "2015-52", "2016-01", "2016-02", "2016-03", "2016-04", "2016-05", "2016-07", "2016-08", "2016-10", "2016-11", "2016-12", "2016-13", "2016-14", "2016-15", "2016-16", "2016-17", "2016-18", "2016-19", "2016-20", "2016-21", "2016-22", "2016-23", "2016-24", "2016-25", "2016-26", "2016-27", "2016-28", "2016-29", "2016-30", "2016-31", "2016-32"];

to order it follows

var weekYear = ["2015-30", "2015-31", "2015-32", "2015-33", "2015-34", "2015-35", "2015-36", "2015-37", "2015-38", "2015-39", "2015-40", "2015-41", "2015-42", "2015-43", "2015-44", "2015-45", "2015-46", "2015-47", "2015-48", "2015-49", "2015-50", "2015-51", "2015-52", "2016-01", "2016-02", "2016-03", "2016-04", "2016-05", "2016-06", "2016-07", "2016-08", "2016-09", "2016-10", "2016-11", "2016-12", "2016-13", "2016-14", "2016-15", "2016-16", "2016-17", "2016-18", "2016-19", "2016-20", "2016-21", "2016-22", "2016-23", "2016-24", "2016-25", "2016-26", "2016-27", "2016-28", "2016-29", "2016-30", "2016-31", "2016-32"];

I tried using this function but it does not work :

monthYear = monthYear.sort(sortNumber);

function sortNumber(elem1, elem2){
        var as = elem1.split('-');
        var bs = elem2.split('-');
        if(parseInt(as[0])< parseInt(bs[0])){
          if(parseInt(as[1]) < parseInt(bs[1])){
            return -1;
          }else if(parseInt(as[1]) > parseInt(bs[1])){
            return 1;
          }else{
            return 0;
          }
        }else if(parseInt(as[0]) > parseInt(bs[0])){
          return 1;
        }
        return 0;
      }

Similarly with the weeks , I wonder if the function is wrong or if there is an easier way to do it .

like image 873
Fabian Sierra Avatar asked Aug 13 '26 11:08

Fabian Sierra


1 Answers

You can create a Date out of your strings and sort them acccordingly:

var monthYear = ["1-2016", "7-2015", "7-2016", "3-2016", "8-2016", "2-2016", "6-2016", "8-2015", "5-2016", "4-2016", "9-2015", "10-2015", "11-2015", "12-2015"];

var sorted = monthYear.sort(function(a,b) {
    a = a.split("-");
    b = b.split("-")
    return new Date(a[1], a[0], 1) - new Date(b[1], b[0], 1)
});
console.log(sorted);
// --> ["7-2015", "8-2015", "9-2015", "10-2015", "11-2015", "12-2015", "1-2016", "2-2016", "3-2016", "4-2016", "5-2016", "6-2016", "7-2016", "8-2016"]

And to sort the week:

Please refer to @mplungjan's answer

like image 162
baao Avatar answered Aug 16 '26 01:08

baao