排序具有年和月的字符串数组

时间:2016-04-13 11:20:31

标签: javascript sorting

我有一个如下所示的数组

var testArr = ["2009-feb", "2009-jan", "2010-mar", "2010-jan", "2011-jul", "2011-sep", "2011-jan", "2012-jan", "2012-dec", "2012-feb", "2013-may", "2013-jul", "2013-jun", "2014-jan", "2014-dec", "2014-may", "2015-may", "2015-jan", "2015-jun", "2016-jan", "2016-dec"];

我需要按照

对它们进行排序
var sortOrder="asc";
testArr.sort(function (a, b) {
    if (sortOrder == "asc") {
        return a.localeCompare(b);
    }
    else {
        return b.localeCompare(a);
    }
});

输出是

["2009-feb","2009-jan","2010-jan","2010-mar","2011-jan","2011-jul","2011-sep","2012-dec","2012-feb","2012-jan","2013-jul","2013-jun","2013-may","2014-dec","2014-jan","2014-may","2015-jan","2015-jun","2015-may","2016-dec","2016-jan"]   

这是错误的。

6 个答案:

答案 0 :(得分:1)

我猜你的意思是按日期排序

int *array;
array=malloc(4*sizeof(int);
for(int i=0;i<4;i++)
  printf("Address %d : %p\n",i,(array+i));

如果你想要它最新的日期第一个翻转日期A与dateB。

在补品上运行示例: https://tonicdev.com/lipp/sort-dates

答案 1 :(得分:1)

试试这个

<强> FIDDLE DEMO

var sortOrder="asc";
testArr.sort(function (a, b) {
    if (sortOrder == "asc") {
        return new Date(Date.Parse(b)) - new Date(Date.Parse(a));
    }
    else {
        return new Date(Date.Parse(a)) - new Date(Date.Parse(b));
    }
});

答案 2 :(得分:0)

对于第一个数组,请尝试此

var monthArray = ["jan","feb", "mar", "apr", "may", "june", "july", "aug", "sep", "oct", "nov", "dec"];

testArr.sort(function(a,b){
  var ayearmonth = a.split("-");
  var byearmonth = b.split("-");
  var montha = monthArray.indexOf(ayearmonth);
  var monthb = monthArray.indexOf(byearmonth );
  if(ayearmonth[0]  != byearmonth[0] )
  {
     return ayearmonth[0] - byearmonth[0];
  }
  {
    return montha-monthb;
  }
});

用于第二阵列的类似方法。

答案 3 :(得分:0)

您可以使用一个分隔日期字符串的函数,并返回一个包含年份和月份数字的数组。月份取自具有月份名称的对象作为哈希值。

function yearMonth(a, b) {
    function getDate(f) {
        var d = f.split('-');
        d[1] = {
            jan: 1, feb: 2, mar: 3, apr: 4, may: 5, jun: 6, jul: 7, aug: 8, sep: 9, oct: 10, nov: 11, dec: 12
        }[d[1].substring(0, 3).toLowerCase()] || 0;
        return d;
    }

    var aa = getDate(a),
        bb = getDate(b);

    return aa[0] - bb[0] || aa[1] - bb[1];
}

var testArr = ["2009-feb", "2009-jan", "2010-mar", "2010-jan", "2011-jul", "2011-sep", "2011-jan", "2012-jan", "2012-dec", "2012-feb", "2013-may", "2013-jul", "2013-jun", "2014-jan", "2014-dec", "2014-may", "2015-may", "2015-jan", "2015-jun", "2016-jan", "2016-dec"],
    testArr1 = ["2009-February", "2009-January", "2010-March", "2010-January", "2011-July", "2011-September", "2011-January", "2012-January", "2012-December", "2012-February", "2013-May", "2013-July", "2013-June", "2014-January", "2014-December", "2014-May", "2015-May", "2015-January", "2015-June", "2016-January", "2016-December"];

testArr.sort(yearMonth);
testArr1.sort(yearMonth);

document.write('<pre>' + JSON.stringify(testArr, 0, 4) + '</pre>');
document.write('<pre>' + JSON.stringify(testArr1, 0, 4) + '</pre>');

答案 4 :(得分:0)

您可以编写自己的自定义comparison方法并将其传递给sort方法。这是一个简单的演示。

var months = [ "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"]

var testArr = ["2009-feb", "2009-jan", "2010-mar", "2010-jan", "2011-jul", "2011-sep", "2011-jan", "2012-jan", "2012-dec", "2012-feb", "2013-may", "2013-jul", "2013-jun", "2014-jan", "2014-dec", "2014-may", "2015-may", "2015-jan", "2015-jun", "2016-jan", "2016-dec"];
var testArr1 = ["2009-February", "2009-January", "2010-March", "2010-January", "2011-July", "2011-September", "2011-January", "2012-January", "2012-December", "2012-February", "2013-May", "2013-July", "2013-June", "2014-January", "2014-December", "2014-May", "2015-May", "2015-January", "2015-June", "2016-January", "2016-December"];  

var sortOrder="asc";
var compare = function(a, b) {
  var aParts = a.toLowerCase().split("-");
  var bParts = b.toLowerCase().split("-");
  
  var dateA = new Date(parseInt(aParts[0]), months.indexOf(aParts[1].substring(0, 3)));
  var dateB = new Date(parseInt(bParts[0]), months.indexOf(bParts[1].substring(0, 3)));
  
  if (sortOrder === "asc")
    return dateA - dateB;
  else
    return dateB - dateA;
}

var sortedTestArr = testArr.sort(compare);
var sortedTestArr1 = testArr1.sort(compare);


document.write('<pre>' + JSON.stringify(sortedTestArr, 0, 4) + '</pre>');
document.write('<pre>' + JSON.stringify(sortedTestArr1, 0, 4) + '</pre>');

答案 5 :(得分:0)

使用Underscore JS按日期排序

例如: -

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct Date {
    int day, month, year;
};

void dateFromString(char* input, struct Date* output) {
    char tmp[3];
    tmp[2] = '\0';
    memcpy(tmp, &input[0], 2);
    output->day = atoi(tmp);
    memcpy(tmp, &input[2], 2);
    output->month = atoi(tmp);
    memcpy(tmp, &input[4], 2);
    output->year = atoi(tmp);
}

void stringFromDate(struct Date* input, char* output) {
    sprintf(output,"%02i%02i%02i",input->day,input->month,input->year);
}

int main() {
    char text[] = "120456";
    struct Date d;
    dateFromString(text, &d);

    printf("%i\n",d.day);
    printf("%i\n",d.month);
    printf("%i\n",d.year);

    char buffer[7];
    stringFromDate(&d,buffer);
    printf("%s\n",buffer);
}