我想根据月份对数组进行排序。日期采用字符串格式: -
datesArray = [
"May-17-2018",
"Jan-06-1",
"Nov-29-2018",
"Nov-01-2018",
"Apr-12-2018",
"Aug-09-2018",
"Feb-18-2018",
"Feb-08-2018",
"Jan-28-2018",
"Jan-26-2018",
"Mar-15-2018",
"Oct-04-2018",
"Jan-25-2018",
"Jun-14-2018",
"Dec-27-2018",
"Jan-29-2018",
"Jan-21-2018",
"Jan-16-2018",
"Jan-20-2018",
"Mar-22-2018",
"Feb-01-2018",
"Mar-01-2018",
"Mar-08-2018",
"Apr-26-2018",
"Feb-22-2018",
"Apr-19-2018",
"Mar-29-2018",
"Sep-06-2018",
"Apr-05-2018",
"May-03-2018",
"May-10-2018",
"Feb-15-2018",
"Jul-12-2018"
]
我已实现此代码,但根据此代码对我的号码进行排序。
datesArray.sort(function(a,b) {
a = a.split('-').reverse().join('');
b = b.split('-').reverse().join('');
return a > b ? 1 : a < b ? -1 : 0;
});
告诉我如何做到这一点。我搜索了这么多,但找不到任何结果。
答案 0 :(得分:1)
您可以将给定日期转换为符合ISO 8601的日期,并按字符串对其进行排序。
var array = ["May-17-2018", "Jan-06-2017", "Nov-29-2018", "Nov-01-2018", "Apr-12-2018", "Aug-09-2018", "Feb-18-2018", "Feb-08-2018", "Jan-28-2018", "Jan-26-2018", "Mar-15-2018", "Oct-04-2018", "Jan-25-2018", "Jun-14-2018", "Dec-27-2018", "Jan-29-2018", "Jan-21-2018", "Jan-16-2018", "Jan-20-2018", "Mar-22-2018", "Feb-01-2018", "Mar-01-2018", "Mar-08-2018", "Apr-26-2018", "Feb-22-2018", "Apr-19-2018", "Mar-29-2018", "Sep-06-2018", "Apr-05-2018", "May-03-2018", "May-10-2018", "Feb-15-2018", "Jul-12-2018"];
array.sort(function (a, b) {
function getISO(s) {
var months = { Jan: '01', Feb: '02', Mar: '03', Apr: '04', May: '05', Jun: '06', Jul: '07', Aug: '08', Sep: '09', Oct: '10', Nov: '11', Dec: '12' };
return s.replace(/^(...)-(..)-(....)$/, (_, m, d, y) => [y, months[m], d].join('-'));
}
return getISO(a).localeCompare(getISO(b));
});
console.log(array);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
答案 1 :(得分:0)
使用您月份的地图。
var datesArray = [
"May-17-2018",
"Jan-06-1",
"Nov-29-2018",
"Nov-01-2018",
"Apr-12-2018",
"Aug-09-2018",
"Feb-18-2018",
"Feb-08-2018",
"Jan-28-2018",
"Jan-26-2018",
"Mar-15-2018",
"Oct-04-2018",
"Jan-25-2018",
"Jun-14-2018",
"Dec-27-2018",
"Jan-29-2018",
"Jan-21-2018",
"Jan-16-2018",
"Jan-20-2018",
"Mar-22-2018",
"Feb-01-2018",
"Mar-01-2018",
"Mar-08-2018",
"Apr-26-2018",
"Feb-22-2018",
"Apr-19-2018",
"Mar-29-2018",
"Sep-06-2018",
"Apr-05-2018",
"May-03-2018",
"May-10-2018",
"Feb-15-2018",
"Jul-12-2018"
];
var mapDates = {
"Jan": 1,
"Feb": 2,
"Mar": 3,
"Apr": 4,
"May": 5,
"Jun": 6,
"Jul": 7,
"Aug": 8,
"Sep": 9,
"Oct": 10,
"Nov": 11,
"Dec": 12
};
datesArray.sort(function(a, b) {
if(mapDates[a.split('-')[0]] < mapDates[b.split('-')[0]]) return -1;
if(mapDates[a.split('-')[0]] > mapDates[b.split('-')[0]]) return 1;
return 0;
});
console.log(datesArray);
&#13;
答案 2 :(得分:0)
在比较之前,最好将日期字符串转换为日期对象。
var datesArray = ["May-17-2018", "Jan-06-2017", "Nov-29-2018", "Nov-01-2018", "Apr-12-2018", "Aug-09-2018", "Feb-18-2018", "Feb-08-2018", "Jan-28-2018", "Jan-26-2018", "Mar-15-2018", "Oct-04-2018", "Jan-25-2018", "Jun-14-2018", "Dec-27-2018", "Jan-29-2018", "Jan-21-2018", "Jan-16-2018", "Jan-20-2018", "Mar-22-2018", "Feb-01-2018", "Mar-01-2018", "Mar-08-2018", "Apr-26-2018", "Feb-22-2018", "Apr-19-2018", "Mar-29-2018", "Sep-06-2018", "Apr-05-2018", "May-03-2018", "May-10-2018", "Feb-15-2018", "Jul-12-2018"];
datesArray.sort(function(a,b) {
var date_items = a.split('-');
var date_a = Date.parse( date_items[0] + ' ' + date_items[1] + ' ' + date_items[2])
date_items = b.split('-');
var date_b = Date.parse( date_items[0] + ' ' + date_items[1] + ' ' + date_items[2])
//date_a = Date.parse(a); //You can replace above parser with other appropriate one
//date_b = Date.parse(b);
return date_a - date_b;
});
console.log(datesArray)
答案 3 :(得分:0)
这适用于Chrome。根据它的外观,Chrome的Date
实现接受构造函数的其他参数语法比其他浏览器更多:
datesArray = [
"May-17-2018",
"Jan-06-1",
"Nov-29-2018",
"Nov-01-2018",
"Apr-12-2018",
"Aug-09-2018",
"Feb-18-2018",
"Feb-08-2018",
"Jan-28-2018",
"Jan-26-2018",
"Mar-15-2018",
"Oct-04-2018",
"Jan-25-2018",
"Jun-14-2018",
"Dec-27-2018",
"Jan-29-2018",
"Jan-21-2018",
"Jan-16-2018",
].map(date => new Date(date).toJSON().substr(0, 10)).sort((a,b) => { return a < b ? -1 : (a > b ? 1 : 0) })
&#13;