我们有一组月份数字:
const months = [1,2,3,4,5,6,7,8,9,10,11,12];
我们如何从当月开始对这个数组进行排序?例如
const currentMonth = 9; // September
months.sort((current, next) => {
// ... sorting algorithm here
});
console.log(months); // Prints Array [9,8,7,6,5,4,3,2,1,12,11,10]
你不需要
排序,因为它已经排序了(只是不是你想要的方式(:
const months = [1,2,3,4,5,6,7,8,9,10,11,12];
const currentMonth = 9;
months = months
.slice(currentMonth)
.concat(months.slice(0, currentMonth))
.reverse();
console.log( [1,2,3,4,5,6,7,8,9,10,11,12].map(m => (12 + 9 - m) % 12 + 1) + '' )