Javascript 如何根据月份和日期对该数组进行排序?

Javascript 如何根据月份和日期对该数组进行排序?,javascript,Javascript,我想根据它的月份对这个数组进行排序 ["Month", "Apple", "Banana", "Mango"] 1: (4) ["Apr-08", 30, 0, 0] 2: (4) ["Apr-16", 26, 0, 21] 3: (4) ["Jul-08", 16.25, 9.25, 0] 4: (4) ["Feb-07", 58, 0

我想根据它的月份对这个数组进行排序

["Month", "Apple", "Banana", "Mango"]
1: (4) ["Apr-08", 30, 0, 0]
2: (4) ["Apr-16", 26, 0, 21]
3: (4) ["Jul-08", 16.25, 9.25, 0]
4: (4) ["Feb-07", 58, 0, 0]
5: (4) ["Feb-08", 11, 0, 0]
6: (4) ["Jun-07", 4, 0, 0]
7: (4) ["Jul-07", 2.25, 0, 0]
我期待这个结果排序后,根据它的月份和日期

["Month", "Apple", "Banana", "Mango"]
1: (4) ["Feb-07", 58, 0, 0]
2: (4) ["Feb-08", 11, 0, 0]
3: (4) ["Apr-08", 30, 0, 0]
4: (4) ["Apr-16", 26, 0, 21]
5: (4) ["Jun-07", 4, 0, 0]
6: (4) ["Jul-08", 16.25, 9.25, 0]
7: (4) ["Jul-07", 2.25, 0, 0]
我想我应该创建属于所有月份的变量,并进行比较

var allMonths = ['Jan','Feb','Mar', 'Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
你可以使用这个方法

常量=[
[“Apr-08”,30,0,0],
[“4月16日”,26日,0日,21日],
[“Jul-08”,16.25,9.25,0],
[“2007年2月”,58,0,0],
[“Feb-08”,11,0,0],
[“2007年6月”,4,0,0],
[“2007年7月”,2.25,0,0]
]
const output=ary.sort((a,b)=>新日期(a[0])>新日期(b[0])?1:-1)
console.log(输出)
常量=[
[“Apr-08”,30,0,0],
[“4月16日”,26日,0日,21日],
[“Jul-08”,16.25,9.25,0],
[“2007年2月”,58,0,0],
[“Feb-08”,11,0,0],
[“2007年6月”,4,0,0],
[“2007年7月”,2.25,0,0]
]
常量输出=ary.sort((a,b)=>
新日期(a[0])>新日期(b[0])?1:-1
)
console.log(输出)
让arr=[
[“Apr-08”,30,0,0],
[“4月16日”,26日,0日,21日],
[“Jul-08”,16.25,9.25,0],
[“2007年2月”,58,0,0],
[“Feb-08”,11,0,0],
[“2007年6月”,4,0,0],
[“2007年7月”,2.25,0,0];
var allMonths=[‘一月’、‘二月’、‘三月’、‘四月’、‘五月’、‘六月’、‘七月’、‘八月’、‘九月’、‘十月’、‘十一月’、‘十二月’];
arr.sort((a,b)=>{
返回allMonths.findIndex((e)=>e==a[0].substr(0,3))-allMonths.findIndex((e)=>e==b[0].substr(0,3))
}    

);欢迎使用堆栈溢出!请拿着(你得到了一枚徽章!)通读一下,尤其是你最好的选择是做你的研究,做相关的主题,然后试一试。如果你在做了更多的研究和搜索后陷入困境,无法摆脱困境,请发布一份你的尝试,并明确指出你陷入困境的地方。人们会很乐意提供帮助。将日期转换为时间戳,然后在
sort
回调函数中比较时间戳您是否可以使用常规日期格式而不是“Jan-01”