我们有一个月份数组:
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();
关于javascript - 从当月开始对 "month numbers"数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48256009/