按月 - 年和周 - 年Javascript对数组进行排序

时间:2016-08-22 14:36:30

标签: javascript arrays

我有一个数组如下:

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

与周数类似

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

按顺序排列

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

我尝试使用此功能但不起作用:

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;
      }

与周数相似,我想知道功能是否错误或是否有更简单的方法。

6 个答案:

答案 0 :(得分:3)

您可以从字符串中创建Date并根据其进行排序:

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

并整理一周:

Please refer to @mplungjan's answer

答案 1 :(得分:2)

只需连接填充并返回差异

function pad(num) {
  return String("0" + num).slice(-2);
}

function sortXXYear(elem1, elem2) {
  var as = elem1.split('-'),
      bs = elem2.split('-'),
      aa = as[1]+pad(as[0]), // yyyymm
      bb = bs[1]+pad(bs[0]);
  return aa - bb;
}

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

monthYear.sort(sortXXYear);
console.log(monthYear);

无需使用函数对星期数组进行排序

var yearWeek = ["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"];

yearWeek.sort(); // No need to use special sort for YYYY-WW
console.log(yearWeek);

答案 2 :(得分:1)

const mysort = (a,b) => {
    const [ma,ya] = a.split('-').map(x=>Number(x))
    const [mb,yb] = b.split('-').map(x=>Number(x))
    return ya < yb ? -1 : ya > yb ? 1 : ma < mb ? -1 : ma > mb ? 1 : 0
}

const ywsort = (a,b) => {
    const [ya,wa] = a.split('-').map(x=>Number(x))
    const [yb,wb] = b.split('-').map(x=>Number(x))
    return ya < yb ? -1 : ya > yb ? 1 : wa < wb ? -1 : wa > wb ? 1 : 0
}

monthYear.sort(mysort)
weekYear.sort(ywsort)

答案 3 :(得分:0)

您可以拆分字符串并按优先级排序,而不使用Date

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

monthYear.sort(function sort(a, b) {
    var aa = a.split('-'),
        bb = b.split('-');
    return aa[1] - bb[1] || aa[0] - bb[0];
});

weekYear.sort(function sort(a, b) {
    var aa = a.split('-'),
        bb = b.split('-');
    return aa[0] - bb[0] || aa[1] - bb[1];
});

console.log(monthYear);
console.log(weekYear);

如果9999-99始终使用weekYear模式,则可以使用内置排序进行排序。

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

weekYear.sort();
console.log(weekYear);

答案 4 :(得分:0)

这是工作代码,对于monthYear:

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"];
function sortMonthYear(myarray){
    let temp;
    let tempArr=new Array(myarray.length);
    //make sort friendly
    for (i=0; i<myarray.length; i++){
        temp = myarray[i].split('-');
        if(temp[0]<10){
            temp[0]= "0"+temp[0];
        }
        tempArr[i] =temp[1]+'-'+temp[0];
    }
    //use sort
    tempArr.sort();
    //back to previous format
    for (i=0; i<tempArr.length; i++){
        temp = tempArr[i].split('-');
        if (temp[1].charAt(0)==0){
            temp[1]= temp[1].substring(1);
        }
        tempArr[i] = temp[1]+'-'+temp[0];
    }
    return tempArr;
}
var monthYear = sortMonthYear(monthYear);
console.log(monthYear)

for weekYear a .sort()就足够了

答案 5 :(得分:0)

您可以将数组中的每个日期转换为自定义排序函数中的实际日期,然后按实际日期对其进行排序:

var monthYear = ["June 2017","October 2018","September 2017"];
monthYear.sort(sortByMonthYear);       

function sortByMonthYear(a, b) {
    var as = a.split(' '),
      bs = b.split(' '),
      ad = new Date(as[0] + ' 1,' + as[1]),
      bd = new Date(bs[0] + ' 1,' + bs[1]);
    return ad.getTime() - bd.getTime();
}

请看看:Click here